我的程序是这样的,为了去旧数组中的“0”,把它放到新数组中去。。
public class exercise03{ public static void main(String args[]){ int oldarr[]={1,3,5,0,0,6,7,8,0,9,0,5,6}; int newarr[]=null; newarr=new int[newarr.length-1]; /*问题在这里。。请问新的数组长度怎么定义。。现在这样子会报错。。。Exception in thread "main" java.lang.NullPointerException at exercise03.main(exercise03.java:5) */ int x=0; for (int i=0;i<oldarr.length;i++) { if (oldarr[i]!=0) {System.arraycopy(oldarr,i,newarr,x,1); x=x+1;} } System.out.print("int newarr[]={"); for (int j=0;j<newarr.length-1;j++) System.out.print(newarr[j]+","); System.out.print(newarr[newarr.length-1]+"}");
}
}