package control;
public class MakeArrary 
{ public static int[] Test(int statr,int end)
{
int[] z = null;
if(statr<=end)
{
int y=end-statr;
for(int x=statr,i=0;x<=y;x++,i++)
{
z[0+i]=x;
}
}
else
{
z=null;
}
return z;
}
public static void main(String[] args)
{
int[] z=Test(1,100);
int y=z.length;
for(int x=0;x<=y-1;x++)
{
System.out.print(z[x]);
}
}
}

解决方案 »

  1.   

    貌似test方法返回的数组的第一个元素是null呀。而且如果test方法中不符合statr<=end条件的话,返回值也被置成null了。你在main中直接打印是不安全的。
      

  2.   

    public static int[] test(int statr, int end) {
    int[] z = null;
    if (statr <= end) {
    int y = end - statr;
    z = new int[y];
    for (int x = statr, i = 0; x <= y; x++, i++) {
    z[0 + i] = x;
    }
    } else {
    z = null;
    }
    return z;
    } public static void main(String[] args) {
    int[] z = test(1, 100);
    int y = z.length;
    for (int x = 0; x <= y - 1; x++) {
    System.out.print(z[x]);
    }
    }你的int数组没有初始化长度,而是null,所以会报空指针错误,增加红色的代码
      

  3.   


     public static int[] Test(int statr,int end)
        {
            if(statr<=end)
            {
                int y=end-statr;
                int[] z = new int[y]; //你之前没有只是定义了一个变量,使用的时候必须声明
                for(int x=statr,i=0;x<=y;x++,i++)
                {
                    z[i]=x; //这里没有必要i+0;
                }
                return z;
            }
            else
            {
                throw new IllegalArgumentException(" [statr] can not greater than [end] ! "); 
                //如果参数异常 抛出异常!
            }
        }