Delete element from array

Posted by Julio Diaz on Stack Overflow See other posts from Stack Overflow or by Julio Diaz
Published on 2011-02-23T23:17:36Z Indexed on 2011/02/23 23:25 UTC
Read the original article Hit count: 235

Filed under:
|

Is there a way I can get rid of some elements in an array. for instance, if i have this array

int testArray[] = {0,2,0,3,0,4,5,6}

Is there a "fast" way to get rid of the elements that equal 0

int resultArray[] = {2,3,4,5,6}

I tried this function but I 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[]);
}

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays