2

This is my array: int[] test= new int[] {1,0,1,0,0,0,0,0,0,0}

Now I need to remove all the zeros from this array so it will display like this: Output : {1,1}

I tried this code from this link-StackOverFlow but didn't work for me.

    int j = 0;
    for (int i = 0; i < test.length; i++) {
        if (test[i] != 0)
            test[j++] = test[i];
    }
    int[] newArray = new int[j];
    System.arraycopy(test, 0, newArray, 0, j);
    return newArray;

Please help me to solve this.

5
  • what is the problem ? Commented Dec 24, 2014 at 14:25
  • It wont remove 0 from it simply returns same. Commented Dec 24, 2014 at 14:29
  • I've just tried you code and it works fine! newArray = {1,1} Commented Dec 24, 2014 at 14:34
  • I just use logcat before and after but getting same here. Commented Dec 24, 2014 at 14:39
  • 2
    this code is correct, so, please add the complete code of this method and how you pass arguments to it. Commented Dec 24, 2014 at 14:40

3 Answers 3

3

Use a List instead. Then you can just do list.removeAll(Collections.singleton(0));. Arrays are far harder to use for this kind of thing.

Example:

List<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 0, 2, 0, 3, 0, 0, 4));
list.removeAll(Collections.singleton(0));
System.out.println(list);

Output: [1, 2, 3, 4]

Sign up to request clarification or add additional context in comments.

3 Comments

Hi thanks for your time, this code didn't work it shows cannot invoke removeAll(set<Integer>on array type of int[]) :(
No, it won't work on an array. You need to use List<Integer> instead of int[]. It's just my advice. Wait a few minutes and somebody will post an answer using int[], but I wouldn't recommend doing it that way.
yeah got but i need to add that integers dynamically so i used int[], while running program i used like this to add int into array test = new int[] { Count1, Count2, Count3,
2

if you insist to work with array you can use this :

int n = 0;
for (int i = 0; i < test.length; i++) {
    if (test[i] != 0)
        n++;
}

int[] newArray = new int[n];
int j=0;

for (int i = 0; i < test.length; i++) {
    if (test[i] != 0)
       { 
         newArray[j]=test[i]; 
         j++;
       }
}

return newArray;

Or Try to use list :

List<Integer> list_result = new ArrayList<Integer>();
for( int i=0;  i<test.length;  i++ )
{
    if (test[i] != 0)
        list_result.add(test[i]);
}
return list_result;

to parse list :

for( int i=0;  i<list_result.size();  i++ )
{
        system.out.pintln((Integer)list_result.get(i));
}

Comments

0

I'm not familiar with Java, but if there is a Underscore or Lo-dash library you can use; then you can employ .filter functionality;

This code is from Swift;

var numbers = [1,0,1,0,0,0,0,0,0,0]
numbers = numbers.filter({ $0 != 0 })  // returns [1,1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.