public class test33 {
public static void main(String[] args) {
int a[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
// 统计0的个数
int count=0;
for (int i = 0; i < a.length; i++) {
if(a[i]==0) {
count++;
}
}
//定义一个新数组
int b[]=new int[a.length-count];
int j=0;//新数组下标
for (int i = 0; i < a.length; i++) {// 遍历数组
if(a[i]!=0) {// 假如不等于0
b[j]=a[i];// 赋值给新数组
j++;
}
}
//打印新数组
for (int i : b) {
System.out.print(i+" ");
}
}
}
运行结果