Reputation: 293
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?
Upvotes: 0
Views: 1762
Reputation: 1
How about this
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);
Upvotes: 0
Reputation: 4476
Using Java 8 :
int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0}
int[] result = Arrays.stream(test).filter(i -> i != 0).toArray();
Upvotes: 1
Reputation: 718768
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
.
Upvotes: 1
Reputation: 104514
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;
}
Upvotes: 1
Reputation: 9375
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;
}
}
Upvotes: 1