0

was curious on how to write a method to remove all zeros from an array. If I have the array in the main method. for example my Main Method would look something like

  public static void main(String[] args) {
 int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0};
 System.out.println(Arrays.toString(test) +  ": length = " + test.length);
 int[] result = removeZeros(test);
 System.out.println(Arrays.toString(result) + ": length = " + result.length);
 }

and have the code output the length and the array without the zeros like:

 [1, 0, 4, 7, 0, 2, 10, 82, 0]: length = 9
 [1, 4, 7, 2, 10, 82]: length = 6

I don't know how to write a method for this other than doing something like this:

  int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0};
    int length = 0;
    for (int i=0; i<test.length; i++){
        if (test[i] != 0)
            length++;
    }
    int [] intResult = new int[length];
    for (int i=0, j=0; i<test.length; i++){
        if (test[i] != 0) {
            intResult[j] = test[i];
            j++;
        }
    }

any ideas how to make this a method and have it print out both the original array and the new array without zeros in it + the length?

4
  • 2
    your code seems ok, I don't understand what problem you are facing Commented Mar 12, 2016 at 2:48
  • @njzk2 I guess i was just curious if that would work haha, so all I have to do is put that in a method? :)? Commented Mar 12, 2016 at 2:49
  • @njzk2 the code that i wrote is in the main method, and I want to put it into a seperate method.. if that makes sense Commented Mar 12, 2016 at 2:50
  • You just need a method public int[] removeZeros(int[] input) {} Commented Mar 12, 2016 at 2:51

5 Answers 5

1

I didn't test it, but this should work:

public class Blah {

public static void main(String[] args) {
 int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0};
 System.out.println(Arrays.toString(test) +  ": length = " + test.length);
 int[] result = removeZeros(test);
 System.out.println(Arrays.toString(result) + ": length = " + result.length);
 }

public int[] removeZeros(int[] test) {
    int length = 0;
for (int i=0; i<test.length; i++){
    if (test[i] != 0)
        length++;
}
int [] intResult = new int[length];
for (int i=0, j=0; i<test.length; i++){
    if (test[i] != 0) {
        intResult[j] = test[i];
        j++;
    }
 return intResult;
}

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

Comments

1

With only the slightest changes to your own code, it's this simple to make it a method.

int [] removeZeros(int [] test);
{
    if (test == null) {
        return null;
    }

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

    int [] intResult = new int[length];

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

    return intResult;
}

Comments

1

any ideas how to make this a method and have it print out both the original array and the new array without zeros in it + the length?

There is no significantly better way to remove the zeros. Obviously, you can put it in a method ... if that's what you want to do. The method needs to create and return the new array. (You can't change the size of the array argument passed to the method ...)

To print an array, either use a loop to iterate and print the elements, or Arrays.toString(array) and output the string.

To print an array's length, print array.length.

Comments

1

Using Java 8 :

int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0}
int[] result = Arrays.stream(test).filter(i -> i != 0).toArray();

Comments

0

How about this

  • create array result with same length of array input
  • use variable length to count length of expected result
  • if the current element of input more than zero, result[length] =
    current element of input test[i] and length++
  • If the length more than 0 then cut array result using value of length

The code :

int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0};
int[] intResult = new int[test.length];
int length = 0;
for (int i=0; i<test.length; i++){
    if (test[i] > 0) {
        intResult[length] = test[i];
        length++;
    }
}
if(length > 0)intResult = Arrays.copyOf(intResult, length);

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.