-1

I have an array of objects in a class, call it passengers. I initialized the array with the x number of passengers, and that would make the array with length x, full of nulls. I need to get a method from the class where I can substitute the next null value for an object. What I'm doing right now is running through the whole array with a for loop and finding the first null value, then changing it to the object.

       if(passenger == null){
        // add a new passenger to this position in the array
        }   
     }

What I was wondering is if is there any built-in method that would make this faster, where I could just substitute the next null value in an array, for a value. At the moment, I'm using Java 7, so there might be a Java 8 option, but it wouldn't work in my case.

knittl
  • 246,190
  • 53
  • 318
  • 364
KuroSuzume
  • 94
  • 1
  • 13
  • 1
    You could keep track of the next-null index. Or implement something like a ringbuffer. – knittl Oct 19 '22 at 20:03
  • 3
    Is using array list out of the question? If you must use primitive array and since you don't remove but only add, I would use a count variable to track the number of items in the array and as index of the last empty slot in the array. To add, a[count++] = item. To process the array, use a for loop with i – Cheng Thao Oct 19 '22 at 20:13
  • 1
    check this https://stackoverflow.com/a/23696571/2774232 – corneliu Oct 19 '22 at 20:25
  • @knittl I think your suggestion was the best one, to have a tracker to avoid having to go through the loop would be a fast and efficient solution. For small arrays like the one, I'm working on it wouldn't make much difference, but for bigger arrays, this tracker would probably be the way to go. can you make your comment into an answer? – KuroSuzume Oct 24 '22 at 19:09

2 Answers2

1

You can introduce a counter which keeps track of the next index with a null value. Every time you add a new element to the array, you increase the index.

Or you could use an ArrayList which does the bookkeeping for you already. It automatically resizes the underlying array too if it needs to hold more elements.

knittl
  • 246,190
  • 53
  • 318
  • 364
-2

If all not null passengers are different objects, you can add all array of passengers to HashSet. In Set you can have only one null. If Set contains null value

Count of nulls = Array length - Set size + 1.

Example on String:

String[] passengers=new String[5];
        passengers[0]="aaa";
        passengers[1]="bbb";
        passengers[2]=null;
        passengers[3]=null;
        passengers[4]=null;
        System.out.println("array size is "+passengers.length);
        HashSet passengersSet=new HashSet();
        passengersSet.addAll(Arrays.asList(passengers));
        System.out.println("set size is "+passengersSet.size());
        if(passengersSet.contains(null)) {
            System.out.println("you need add " +(passengers.length-passengersSet.size()+1));
        }
        else{
            System.out.println("There are no null-passengers");
        }