I'm working on a problem that gives an array of objects (4 objects) in an array with a size of 7, so there are 3 null values. The array is scrambled. I need to consolidate the elements so they are in the 0,1,2,3 spaces of the array, in the same order. the null values have to be put at the end of the array on spaces 4,5,6. Here is what I have so far:
//a car object has a name and top speed
public void consolidate() {
int numCars = 0;
for (int i = 1; i < cars.length; i++) {
if (cars[i] != null)
numCars++;
}
for (int k = 0; k < numCars; k++) {
for (int i = 1; i < cars.length; i++) {
if (cars[i - 1] == null) {
cars[i - 1] = cars[i];
cars[i] = null;
}
}
}
}