完善下面的代码,
把数组中所有连续二个以上的0去掉,将结果存入一个新数组,如
0012010001----->1201package day02;
public class B {
public static void main(String[] args) {
int[] srcArray={0,0,1,2,0,1,0,0,0,1};
int[] descArray=new int[50];
int i=0,j=0,count=0;
for(i=0;i<srcArray.length;){
count=0;
if(srcArray[i]!=0){
//(1)
i++;
}else{
while(i<srcArray.length&&(2)){
//(3)
i++;
}
if(count<2){
//(4)
}
}
}
for(i=0;i<j;i++){
System.out.println(descArray[i]);
}

}
}大家帮我看下,thanks

解决方案 »

  1.   


    import java.util.*;
    public class Test
    {
    public static void main(String args[]){
    int[] srcArray={0,0,1,2,0,1,0,0,0,1};
    List<Integer> list = new ArrayList<Integer>();
    for(int i=0;i<srcArray.length;i++){
    if(srcArray[i]==0 && srcArray[i+1]==0){
    if(srcArray[i+2]==0){
    list.add(srcArray[i+3]);
    i = i+3;
    }else{
    list.add(srcArray[i+2]);
    i = i+2;
    }
    }else{
    list.add(srcArray[i]);
    }
    } Iterator<Integer> it = list.iterator();
    while(it.hasNext()){
    System.out.print(it.next());
    }
    }
    }
      

  2.   

    public class B {
    public static void main(String[] args) {
    int[] srcArray = { 0, 0, 1, 2, 0, 1, 0, 0, 0, 1 };
    int[] descArray = new int[50];
    int i = 0, j = 0, count = 0;
    for (i = 0; i < srcArray.length;) {
    count = 0;
    if (srcArray[i] != 0) {
    descArray[j++] = srcArray[i];
    i++;
    } else {
    while (i < srcArray.length && (srcArray[i]==0)) {
    ++count;
    i++;
    }
    if (count < 2) {
    descArray[j++] = srcArray[i-1];
    }
    }
    }
    for (i = 0; i < j; i++) {
    System.out.print(descArray[i]);
    } }
    }
      

  3.   

    (1) descArray[j++]=srcArray[i]
    (2) srcArray[i] == 0
    (3) count++
    (4) descArray[j++]=0;
      

  4.   

    public class A {
    public static void main(String[] args) {
    int[] array = { 0, 0, 0, 1, 0, 1, 0, 0, 1, 2, 0, 1 };
    Object[] getArray = getObject(array);
    for(int i = 0;i < getArray.length;i++) {
    System.out.print(getArray[i]+",");
    }
    } public static Object[] getObject(int[] array) {
    List<Integer> list = new ArrayList<Integer>();
    Object[] getArray = new Object[array.length];
    int temp = 0;
    for (int i = 0; i < array.length;) {
    if (array[i] == 0) {
    for (int j = i; array[j] == 0; j++) {
    temp = j;
    }
    if ((temp - i) >= 1) {
    i = temp + 1;
    } else {
    list.add(array[i]);
    i++;
    }
    } else {
    list.add(array[i]);
    i++;
    }
    }
    getArray = list.toArray();
    return getArray;
    }
    }我也写了一个。