public class TEST {
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 [] newArr= new int[oldArr.length] ;
int n=0;
for (int i=0;i<oldArr.length;i++) {
if(oldArr[i]==0) {}
else {
newArr[n]=oldArr[i];
n++;
}
}
for(int i=0;i<newArr.length;i++)
{
if(newArr[i]!=0) {System.out.print(newArr[i]);}
}
}
}
注意int [] newArr={};会报错,因为没有固定的长度
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at TEST.main(TEST.java:11)
原文地址:https://www.cnblogs.com/otakus/p/12165858.html
时间: 2024-10-29 02:41:34