Can an arraylist hold null reference or not using add(Index i, Object obj) method?
posted 11 years ago
Hi, while using overloaded add method(using 2 parameters) of array list class, can’t I place deliberately an object at any position in array list which is not in sequence or order. For e.g- I kept on adding the objects using simple add method(which takes one argument i.e. object as parameter) like
By doing this can’t I think my array list in the end would be something like:-
One|Two|null|Three
I tried above case but failed on runtime although my compilation was successful. Does it mean that an array list can’t hold null reference? If No, then I again tried like num.add(«») is this same as null reference or not? Please clear my basic concept.
Marshal
posted 11 years ago
Vinod Vijay wrote: By doing this can’t I think my array list in the end would be something like:-
One|Two|null|Three
You can think that if you like, but it isn’t the case. So it wouldn’t be a good idea to think it.
I tried above case but failed on runtime although my compilation was successful. Does it mean that an array list can’t hold null reference?
No, the code you used didn’t add any nulls to the list, so it didn’t test the question of whether an ArrayList could contain nulls.
If No, then I again tried like num.add(«») is this same as null reference or not?
No, an empty string is not the same as a null reference. Here’s how you represent null in Java:
You certainly chose a complex and roundabout way to find out whether an ArrayList can contain nulls. Here’s a simpler way:
posted 11 years ago
Paul Clapham wrote:
You can think that if you like, but it isn’t the case. So it wouldn’t be a good idea to think it.
Why are you saying like that? I tried as suggested by you to use null and it worked. I think array list can hold null references.
No, the code you used didn’t add any nulls to the list, so it didn’t test the question of whether an ArrayList could contain nulls.
Certainly not. However I tried but later commented that statement. //num.add(7,»SEVEN»);
No, an empty string is not the same as a null reference. Here’s how you represent null in Java:
Thanks for clearing this. So this means, null reference means no reference to an object on Java Heap and empty String means there is a reference to an object on Java Heap but for JVM it doesn’t matter what the size of that object is until or unless it is eligible for Garbage Collection. Please correct me if I’m wrong.
You certainly chose a complex and roundabout way to find out whether an ArrayList can contain nulls. Here’s a simpler way:
Bartender
posted 11 years ago
Vinod Vijay wrote: Can’t I place deliberately an object at any position in array list which is not in sequence or order.
I tried above case but failed on runtime although my compilation was successful. Does it mean that an array list can’t hold null reference? If No, then I again tried like num.add(«») is this same as null reference or not? Please clear my basic concept.
If you look at the definition for List.add(int, E), you’ll see that it says:
«Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).»
and also specifies that it throws IndexOutOfBoundsException «if the index is out of range» (ie, < 0 || >size()).
All of which mean that you can’t insert elements at arbitrary points (which is, in effect, what you’re hoping to do).
There is however, a structure that allows you do do exactly what you want:
Map
and it’ll likely take a lot less space if your elements are widely spaced too.
«Leadership is nature’s way of removing morons from the productive flow» — Dogbert
Articles by Winston can be found here
posted 11 years ago
You would see an output like [1, 2, null, 4]
And yes arraylist can hold null references
posted 11 years ago
You would see an output like [1, 2, null, 4]
And yes arraylist can hold null references
Thanks Manjunath. When I looked at your code first without running it, I thought there is some mistake in the sequence which you had shown like [1, 2, null, 4]
because null reference would have replaced the object completely on position 2nd (object 4) but then I thought there is another method of array list class called set() which is used to update the object by replacing it when we provide the index position and new object value to be replaced, so here in this case, it is gonna insert it in between 2 and 4 where 4 will move towards right giving space to null.
I have verified it by running it as well. Yeah, I will remember now that an array list can hold null references as well.
Could you please confirm this also if I’m wrong in below statement:-
Vinod Vijay wrote:
Thanks for clearing this. So this means, null reference means no reference to an object on Java Heap and empty String means there is a reference to an object on Java Heap but for JVM it doesn’t matter what the size of that object is until or unless it is eligible for Garbage Collection. Please correct me if I’m wrong. [/code]
Interface List
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2) , and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.
The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator , add , remove , equals , and hashCode methods. Declarations for other inherited methods are also included here for convenience.
The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.
The List interface provides a special iterator, called a ListIterator , that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.
The List interface provides two methods to search for a specified object. From a performance standpoint, these methods should be used with caution. In many implementations they will perform costly linear searches.
The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.
Note: While it is permissible for lists to contain themselves as elements, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a list.
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. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException . Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the list may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as «optional» in the specification for this interface.
Unmodifiable Lists
- They are unmodifiable. Elements cannot be added, removed, or replaced. Calling any mutator method on the List will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the List’s contents to appear to change.
- They disallow null elements. Attempts to create them with null elements result in NullPointerException .
- They are serializable if all elements are serializable.
- The order of elements in the list is the same as the order of the provided arguments, or of the elements in the provided array.
- The lists and their subList views implement the RandomAccess interface.
- They are value-based. Programmers should treat instances that are equal as interchangeable and should not use them for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. Callers should make no assumptions about the identity of the returned instances. Factories are free to create new instances or reuse existing ones.
- They are serialized as specified on the Serialized Form page.
This interface is a member of the Java Collections Framework.