Reputation: 39
So I am in the AP Computer Science class in High School so I'm not very experienced. I have a program to do that requires me to read in numbers from a file, put those numbers into an array and then remove all of the 0's from the array.
So if the numbers are: 0,2,4,6,0,5,3,5... I would need to create an array: [0,2,4,6,0,5,3,5] and then remove the 0's: [2,4,6,5,3,5]
I have to do this using arrays and I am not allowed to create a second array to do this. I have looked all over online and the Java API's to find a method that can remove an element from an array but I simply can't find one. If somebody has any idea of one that I can use or a direction to steer me in, your advice would be much appreciated.
THIS IS THE QUESTION WORD FOR WORD: 1. Write a program that reads a text file(compact.txt) and stores the integers in an array. Your instructor will provide this text file. 2. Write a method compact that removes all zeroes from the array, leaving the order of the elements unchanged. All local variables within this function must be scalar. In other words, you may not use a second array to solve the problem. 3. Do not solve the problem simply by printing out only the non-zero values in the array. The compact method must remove all zeroes from the array.
Upvotes: 2
Views: 2580
Reputation: 40036
Another Edit:
(I think previous answer is still meaningful and I am keeping it at the end. This edit is mainly for suggestion specific to the homework requirement)
Base on the question, the compact logic treat 0 as something that is "meaningless" and need to be "removed". Therefore we don't really need some kind of special value after we "shrink" the array. Simply keeping it as 0 will help.
Apart from the "copy [i+1,end] to i" method, there is another (easier and possibly faster) method that you can do to "remove" the zeros.
Basically what you need is, iterate through the array. If you encounter a 0, then find the first non-zero value after that position, and swap the zero with that non-zero value.
Which looks like this in psuedo code:
for (i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
for (j = i+1; j < arr.length; j++) {
if (arr[j] != 0) {
arr[i] = arr[j];
arr[j] = 0;
break;
}
}
}
}
// arr is "shrinked" here
Then it is your choice to return an actually shrinked copy of array, or simply return the "so-called-shrinked" array with 0s at the end.
Something for you to think of:
First, in Java, size of array is fixed, therefore, it is not possible to shrink array's size. Therefore, it is IMPOSSIBLE to have a result array with less elements without creating a new array
If it is fine for you to leave unused element at the end as some special values (e.g. -ve, or 0 etc), the removing element at position i
from array essentially means:
copy array elements [i+1 to end] to position i, and replace arr[end] with special empty value
e.g. [1, 3, 5, 7, 9]
If I want to remove index 2, what I need to do is to copy element 3-4 to position 2:
[1,3,5,7,9] -> [1,3,7,9,9]
^^^ ^^^
and replace end element with some special value (e.g. -1 in this example):
[1,3,7,9,9] -> [1,3,5,7,-1]
Array copy can be done easily by using System.arrayCopy()
Have just seen update in your question.
Most of my answer is still valid, and here is some extra update with regards to your question:
Arrays.copyOf(oldArray, newLength)
.Here is a piece of psuedo-code for point 3:
int[] compact(int[] input) {
int arrSize = input.length;
int i = 0;
while (i < arrSize) {
if (input[i] == 0) {
copy input[i+1 to end] to input[i to end-1]
arrSize--;
} else {
i++;
}
}
return Arrays.copyOf(input, arrSize);
}
Upvotes: 1
Reputation: 26574
I'm not normally a fan of doing homework, but I found this one interesting and impossible as written, so here goes.
This is much like Christian's answer, but I'm using Integer instead of int so that I can set the 0s to null instead of some other integer. I've also avoided the extra loop he has to copy every remaining value down on every single 0, instead iterating the array once and then iterating the tail only once to set the null values.
public class ArrayCompact {
private static Integer[] ARRAY = { 1, 3, 5, 0, 7, 9, 0, 2, 0, 4, 6, 0, 8, -1, 0 };
public static void main( String[] args ) {
printArray( compact(ARRAY ));
}
public static Integer[] compact( Integer[] ints ) {
int j = 0;
for ( int i = 0; i < ints.length; i++ ) {
if ( ints[i] != 0 ) {
ints[j++] = ints[i];
}
}
for ( int i = j; i < ints.length; i++ ) {
ints[i] = null;
}
return ints;
}
public static void printArray( Integer[] ints ) {
for ( Integer i : ints ) {
System.out.print( i + " " );
}
}
}
Output 1 3 5 7 9 2 4 6 8 -1 null null null null null
Technically I guess you could just not print the nulls, since that's not not printing 0s...
Upvotes: 2
Reputation: 34146
You could try the following: Since you cannot modify the length of the array, you can arrange it so you put all zeroes at the end of the array and with value -1
(this is optional, just to indicate they are zeroes).
public static void main(String[] args)
{
int[] arr = { 0, 1, 2, 0, 3, 0, 4, 0, 5, 6, 7 };
int[] arrWithoutZeros = compact(arr);
for (int i : arrWithoutZeros) {
System.out.println(i);
}
}
private static int[] compact(int[] arr)
{
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
int j = 0;
for (j = i; j < arr.length - 1; j++) {
arr[j] = arr[j + 1];
}
arr[j] = -1;
i--;
}
}
return arr;
}
Output:
1
2
3
4
5
6
7
-1
-1
-1
-1
Note: This meets the question requirements:
-1
)Upvotes: 2