5

I just had a simple question that google is being unable to resolve. So if I set some of the elements of an ArrayList are set to null, will they still be counted in list.size(), and will the indices for the remaining elements still remain intact?

If not, can any of you advise as to how I can do that?

5
  • 7
    Yes and yes. Some test code would have shown you that :) Commented Feb 24, 2014 at 19:53
  • Yes, they will. You could have noticed this yourself if you would have taken the time to type a few lines of code. You will have to manually loop over the list to create a new one with only the non-empty entries. Commented Feb 24, 2014 at 19:53
  • Why would one want to set elements of a list to null?! Commented Feb 24, 2014 at 19:53
  • Perhaps I have to be enlighted, but I don't see any use. Commented Feb 24, 2014 at 19:54
  • trying it would have taken you less time than to ask the question Commented Feb 24, 2014 at 20:07

5 Answers 5

6

Yes, take a look on a add() method implementation, for example:

public boolean add(E object) {
    Object[] a = array;
    int s = size;
    if (s == a.length) {
        Object[] newArray = new Object[s +
                (s < (MIN_CAPACITY_INCREMENT / 2) ?
                 MIN_CAPACITY_INCREMENT : s >> 1)];
        System.arraycopy(a, 0, newArray, 0, s);
        array = a = newArray;
    }
    a[s] = object;
    size = s + 1;
    modCount++;
    return true;
}

Since ArrayList.size() returns just the value of size variable, it's clear, that if you add null, the size will be increased by 1, so, yes, null is counted.

Sign up to request clarification or add additional context in comments.

Comments

2

You can remove all the elements from your list

MyList.removeAll(Collections.singleton(null));
MyList.size();

Reference: http://www.mhaller.de/archives/12-How-to-remove-all-null-elements-from-a-Collection.html

1 Comment

Yes, you can, although that doesn't have anything to do with this question.
2

Yes and Yes, you can verify it with this simple code

public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        list.add("" + i);
    }
    System.out.println(list.size() + " " + list);
    list.set(0, null);
    System.out.println(list.size() + " " + list);
}

10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
10 [null, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Comments

1

Yes, the null element will still be counted in the size of the list and the indices of the remaining element will remain intact.

Comments

0

It's a bad idea to infer class behaviour from test code or reading the source code, also because ArrayList is not final and the class you obtain may be an extended version of ArrayList with a different implementation.

Just read List and ArrayList contracts, that must be mantained in the extending classes:

List:

An ordered collection (also known as a sequence). ... Some list implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements.

ArrayList:

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.

the contract says that ArrayList permits null elements, so the answer to you question is yes, the list size and indices remains intact.

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.