2013年9月22日星期日

White distress , the array problem , hope god wing , the code below

public class Test003 {

/ **
* @ param args
* /
public static void main (String [] args) {
/ / TODO Auto- generated method stub
int oldArr [] = {1, 3, 4, 5, 0, 0, 6, 6, 0 , 5, 4 , 7, 6 , 7, 0, 5 } ;
int newArr [] = new int [12];
for (int i = 0; i if (oldArr [i] == 0) {
i + +;
continue;
}
int j = 0;
newArr [j] = oldArr [i];
j + +;
}
for (int k = 0; k System.out.print (newArr [k] + "");
}
}
I want to implement a function to create a new array for the result , and then find the original array in a value of 0 , remove , remove 0 after the value is stored in a new array , wrote the following code, why output is 700 billion ? Great God seeking advice, am I wrong ? Also, if the amount of data a lot, certainly impossible to know at the beginning of the array after removing 0 left 12 , how using variables instead of numbers ? Neighborhoods
------ Solution --------------------------------------- -----
loop is executed every time you will perform int j = 0; each newarr [0] = oldarr [i]; therefore begins with 7 , followed by the no value defaults to 0 ;
------ Solution ---------------------------------------- ----
with list it, and then converted into an array
------ Solution ------------------------ --------------------

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[12];
int j = 0;
for (int i = 0; i < oldArr.length; i++) {
if (oldArr[i] == 0) {
continue;
}
newArr[j] = oldArr[i];
j++;
}
for (int k = 0; k < newArr.length; k++)
System.out.print(newArr[k] + " ");
}

------ Solution -------------- ------------------------------

public static void main(String[] args) {
int oldArr[] = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 };
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < oldArr.length; i++) {
if (oldArr[i] == 0) {
continue;
}
list.add(oldArr[i]);
}
for (int k = 0; k < list.size(); k++)
System.out.print(list.get(k) + " ");
}

------ For reference only - -------------------------------------
cycle are wrong.
------ For reference only -------------------------------------- -
issue is resolved, Indian friends to get, but still thank you !

没有评论:

发表评论