8

Well, I have a have a primitive array of objects, and because I can't remove them from the array, I instead change the object's position in the array to null. However, if I want to iterate over each object in the array, in the following way:

for (Derp derp : derps){
     derp.herp++;
}

Do I have to do something like this?

for (Derp derp : derps){
     if (derp != null){
     derp.herp++;
     }
}

Or would it be fine the first way I had it? Will the for loop 'know' that it only has to iterate over the Derp objects, and not the null objects, because I've declared it as a Derp object? Or perhaps it just treats it as a Derp object because I've said it would be, and it would cause an error when it tries to iterate over a non-Derp object? Or is null still a Derp object, just one that is null? Which is it, and what code can I use?

Alternatively, how can I remove an object from a primitive array and not leave a null object and actually shorten the length of the primitive array?

LaneWalker
  • 255
  • 6
  • 14
  • 2
    It will iterate over every element in the array, whether it is null or not, so you need the null check. By the way, this would have been easy to test. – Brigham Oct 29 '13 at 06:33
  • surely iterates over all, no matter nulls or not – wxyz Oct 29 '13 at 06:34
  • @user2901402 Use `ArrayList` instead of `Array` to remove possibility of `NullPointerException` else you have to check not null while iterating Array. Second one of your option is proper for Array. – Not a bug Oct 29 '13 at 06:36

6 Answers6

6

This is better.

for (Derp derp : derps){
     if (derp != null){
     derp.herp++;
     }
}

The first one throws nullpointer exception.if any value is null

how can I remove an object from a primitive array and not leave a null object.

Once memory allocated to that element in array, least you can do is making it null.

actually shorten the length of the primitive array?

No. It is fixed while declaring it self.After the deceleration you cannot change the length.

Arrays died long back. Your best bet is List which have benefites of

  • Positional access — manipulates elements based on their numerical position in the list. This includes methods such as get, set, add, addAll, and remove.
  • Search — searches for a specified object in the list and returns its numerical position. Search methods include indexOf and lastIndexOf.
  • Iteration — extends Iterator semantics to take advantage of the list's sequential nature. The listIterator methods provide this behavior.
  • Range-view — The sublist method performs arbitrary range operations on the list.
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
5

Null is a valid list item so you will need to check for it.

You can not simply remove an element from a primitive Array. You can use a more advanced structure such as ArrayList where elements can be removed. The following two examples are one-liners to remove all NULL values from a List.

list.removeAll(Collections.singleton(null));

or

list.removeAll(Arrays.asList(new Object[]{null})); 
Samuel O'Malley
  • 3,471
  • 1
  • 23
  • 41
1

There are several options:

  1. You could explicitly check for nulls as you do in your second example.

  2. If fast access by index is not important, you could use a LinkedList. Removing an element from a linked list in cheap.

  3. If the ordering of elements is not important, use a Set.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Its better to use the second way because, if there are null values in the array, you'll get a NullPointerException. The thing is, when you encounter a null, the derp object will have a null reference as it is an object.

for (Derp derp : derps){
     if (derp != null){
         derp.herp++;
     }
}

how can I remove an object from a primitive array and not leave a null object and actually shorten the length of the primitive array?

It is not at all a primitive array. It an array of Derp. And by default, all the indexes in the array would have null references, unless you manually initialize each. Thus, once an array memory is allocated, you can't reduce the size, as arrays are static in nature. You need to use an ArrayList, if you want it to grow/shrink dynamically.

Rahul
  • 44,383
  • 11
  • 84
  • 103
0

You can use ArrayList instead of array. It provides methods like add and remove which you are trying to implement using array. So better way is to user ArrayList.

Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
0

How enhanced for loop works

for (Derp derp : derps){
  derp.herp++;
}

converts to

for(Iterator<Derp> i = derps.iterator(); i.hasNext(); ) {
  Derp element = i.next();

}

You need to have null check on collection and if you doing any operation on the elements of that collection than you should have null check on the elements too.

As Iterator is checking elements are there or not , so if null check is not there than you will get NULL POINTER EXCEPTION

Use it like this,

if(derps != null){
  for (Derp derp : derps){
    if(derp != null)
      derp.herp++;
  }
}

Take a look here on how the enhanced for loop is implemented

Community
  • 1
  • 1
Prateek
  • 12,014
  • 12
  • 60
  • 81