2

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;
            }
        }
    }
}
2

4 Answers 4

2

If you are okay with creating a separate array of the same length but null values at the end, you can do it very simply as shown below:

int j=0;
Car[] newCars = new Car[cars.length];
for(int i=0; i<cars.length; i++) {
    if(cars[i] != null) 
        newCars[j++] = cars[i];
}
Sign up to request clarification or add additional context in comments.

5 Comments

I love the simplicity. Its totally ok to create a new array because at the end you can make the old one equal to the new one. cars = newCars;
Creating a copy of the array is a bit inefficient in the purists' view. But sometimes trading a little efficiency for simplicity, readability and maintainability is a good thing in a long run.
cars = newCars is a bad idea. What will happen if you accidentally change newCars in the code flow. You will end up changing cars also. @carson.hcoder
@Gibbs, when you do cars=newCars, both cars and newCars point to the same array (new one). The original array that cars pointed to would be purged by GC.
@VHS That is not always guaranteed correct? OP code is not returning anything. And with this simple method it works as you said.
1
1. Have two variables i = 0, j = last element of array
2. Repeat
      Move i till you get a null object
      Move j till you get a non null object
      swap value of i and j
      If i <= j then break

Hope it helps.

To maintain the same order

1. Have two variables i = 0, j = 0
2. Repeat
      Move i till you get a null object
      j = i+1
      Move j till you get a non null object
      swap value of i and j
      Either i or j reaches end of the element then break

1 null 2 3 null

i => null at index = 1

j = at index = 2

===

1 2 null 3 null

i finds null at index =2 as it was at index=1

j finds 3

1 2 3 null null

3 Comments

Thank you, I think this will work, but in order to have the elements at the beginning of the array wouldn't the "move i" and "move j" be flipped? so "i" looks for null objects, and "j" looks for non-null objects?
This algorithm has two problems I believe. First it moves the non-null to the end not the beginning of the array and it changes the order of the non-null values. Consider the array [1, 2, null], this algorithm would swap 1 and null to produce [null, 2, 1].
Your correction fixes the first problem, but not the second one. It can change the order of the non-null values. Consider [null, 1, 2], it will become [2, 1, null]
1

Alternativley you could use Arrays#sort like below:

Arrays.sort(cars, new Comparator<Car>() {
        @Override
        public int compare(Car c1, Car c2) {
            if (c1 == null && c2 == null) {
                return 0;
            }
            if (c1 == null) {
                return 1;
            }
            if (c2 == null) {
                return -1;
            }
            return Arrays.asList(cars).indexOf(c1)-Arrays.asList(cars).indexOf(c2);
        }
    });

Comments

0

This seems to work:

Integer[] cars = {null, null, 1, null, 2, 3, 4};
int nums = 4;
for (int i = 0; i < nums; i++) {
    if (cars[i] != null) {
        continue;
    }
    int j = i + 1;
    while(j < cars.length - 1 && cars[j] == null) {
        j++;
    }
    cars[i] = cars[j];
    cars[j] = null;
}

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.