现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为
: int newArr [] ={1,3,4,5,6,6,5,4,7,6,7,5}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class homework2 {
    public static void main(String []args) {
         int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
         int count=0;
         for(int i=0;i<oldArr.length;i++) {
             if(oldArr[i]==0) {
                    count++;
             }
         }
         System.out.print("数组中值为0的个数为"+count+"个+\n");
         int a=0;
         int []newArr=new int [oldArr.length];
         for(int j=0;j<oldArr.length;j++) {
            if(oldArr[j]!=0) {
                newArr[a++]=oldArr[j];
            }
         }
         System.out.print("新数组为:\n");
         for(int k=0;k<a;k++) {
             System.out.print(newArr[k]+" ");
         }
    }
}

  运行结果为: