java - Delete element from array -
possible duplicate:
removing element array (java)
is there way can rid of elements in array. instance, if have array
int testarray[] = {0,2,0,3,0,4,5,6}
is there "fast" way rid of elements equal 0
int resultarray[] = {2,3,4,5,6}
i tried function got lost using lists
public int[] getridofzero(int []s){ list<> result=new arraylist<>(); for(int i=0; i<s.length; i++){ if(s[i]<0){ int temp = s[i]; result.add(temp); } } return result.toarray(new int[]); }
java arrays can't resized. need create new array.
count non-zero elements in array. create new array size. copy elements old new array, skipping on 0 elements.
you can lists. best bet create list of integers; add non-zero elements it; use toarray create array list.
Comments
Post a Comment