I need to create a method compact that can remove all zeroes from an array and leave the order of other elements unchanged, but without creating a new array. The method must actually remove all zeroes, not just print out the other elements, and it should only use one private array. So 0, 9, 7, 0, 0, 23, 4, 0 would turn into 9, 7, 23, 4. I've been stuck since everything I have have found online says it is impossible to remove elements from an array without creating a new array, so any help would be much appreciated.

Without creating new array the best you can do is to compress out the zeros, then fill the unused elements at the end of the array with zeros. Ie 9 7 23 4 0 0 0 0
There is no way to change the size of a Java array after it has been created.

By default, when you declare and create a new array, each element is set to 0.
So, using this logic we can conclude that if you remove a 0 from the array, Java replaces it with the default value, 0.

There are two simple options.
1. Bite the bullet use another array
2. If you are returning the values or printing them to the screen, use some sort of check to ensure that only values that are greater than 0 are output.

Those are the only things I can think of. If neither of these suit your specification, then I think you may be at a dead end.

Another idea may be to use the dreaded ArrayList object. Just a thought

good luck

that would depend on the datatype. he can go and use the Integer wrapper class, instead of the int type. that way the default value will be null.
after entering the values, he can simply replace the 0 valued ones by null as well.

ObSys
You can't remove a value from an int array. You can only overwrite the existing value with a new value that you specify. There is no default replacement value.

JamesCherrill you're correct however this seems like a moot point to focus on. Either way, the default value is 0. Whether you can remove items or not is not actually relevant as my response didn't focus on that point.

Don't, really understand the point of your reply...

I have have found online says it is impossible to remove elements from an array without creating a new array, so any help would be much appreciated.

It's possible if you are OK with the old allocated array sticking around. But this might be much more problematic than just creating a new one. The reason being, let's suppose you have an array of 10000 elements of which there are only 50 non-zero elements. If you throw away the old one and create a new array, you'll just be using an array of 50 elements; the old 10K array would be garbage collected. But, if you choose to not allocate a new array, you will now have a 10K size array live in heap with just 50 slots in use which is plain wasteful.

This might be a good idea if you want to forgo allocations but doing so withou a good reason doesn't make sense.

If this is homework, the solution should be a simple for loop over the array.

Edited 3 Years Ago by ~s.o.s~

ObSys:
This entire thread is about removing entries from arrays. You posted "if you remove a 0 from the array, Java replaces it with the default value, 0.".
That statement is completely wrong, and very misleading for the OP and others trying to understand this thread. That's why I commented on it. Please don't be offended, it wasn't personal. J.

This article has been dead for over six months. Start a new discussion instead.