去除整形数组中为0的元素 - iiishooter's Blog - Attitude is Altitude
去除整形数组中为0的元素
iiishooter
posted @ 2010年5月24日 22:56
in Java SE
, 2088 阅读
将整形数组中为0的元素去掉,并将剩余的元素复制到一个新的数组中并输出。
public class Move0 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int array1[]={0,1,2,3,4,0,0,5,6,0,7,8,9,10,11,0}; //初始化数组1 int flag=0; for(int i=0;i<array1.length;i++) //求出数组2的长度 if(array1[i]!=0) { flag++; } int array2 [] =new int [flag]; //定义数组2 //System.out.println(flag); int temp=0; while(temp!=array2.length) //拷贝条件 { for(int j=0;j<array1.length;j++) //开始拷贝 { if(array1[j] != 0) { array2[temp]=array1[j]; temp++; } } } for(int k=0;k<array2.length;k++) //输出结果 System.out.println(array2[k]); } }
输出结果:
1
2
3
4
5
6
7
8
9
10
11
2022年8月24日 20:23
AP Inter Model Question Papers 2023 Download, First let’s check out some details and information about the Model Question Papers, in some states it is also referred to as ‘Important Question Papers’, AP Inter Model Question Paper 2023 the Model Question Paper is the paper form of the students in which all the details of the candidates along with the Sample Question Paper and timings including the photograph are mentioned which is carried out in the examination hall and used for verification purpose.