import java.util.Arrays; public class MainTest { 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[]=Arrays.stream(oldArr).filter(e->e!=0).toArray(); System.out.print(Arrays.toString(newArr)); } }[1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5]本回答被网友采纳
第2个回答 2018-04-02
public class Demo17 { public static void main(String[] args) { int j=0; int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; int newArr[]=new int [oldArr.length]; for(int i=0;i<oldArr.length;i++){ if(oldArr[i]!=0){ newArr[j]=oldArr[i];//元素为0的不存入newArr j++; } } for(int m:newArr){ if(m==0){ break; } System.out.print(m+" ");} } }