G Batra wrote:
1. When subList(int, int) method is called, it returns an instance of AbstractList. Now, if I try to add an element in this sublist by calling add method on AbstractList instance I don't get an exception which is thrown in the AbstractList class in turn the element gets added. Not sure how this is working?
AbstractList's add method calls this method:
G Batra wrote:
2. What's the point of ArrayList's subList method considering the fact that after its invocation:
- It doesn't allow structural modification to the backed List?
G Batra wrote:
2.1 Also, why adding an element to the end of the subList works? Shouldn't the bounds of subList limit the addition of new elements? Think throwing ConcurrentModificationException when an element is added outside its bounds in the sublist make more sense.
Not sure how this is working?
---=== C O D E A B B E Y ===--- my site with programming problems (and Certificates)
G Batra wrote:1. When subList(int, int) method is called, it returns an instance of AbstractList. Now, if I try to add an element in this sublist by calling add method on AbstractList instance I don't get an exception which is thrown in the AbstractList class in turn the element gets added. Not sure how this is working?
G Batra wrote:2. What's the point of ArrayList's subList method considering the fact that after its invocation:
- It doesn't allow structural modification to the backed List?
G Batra wrote:2.1 Also, why adding an element to the end of the subList works? Shouldn't the bounds of subList limit the addition of new elements? Think throwing ConcurrentModificationException when an element is added outside its bounds in the sublist make more sense.
G Batra wrote:1. I think I should have been more specific here. Sorry for not mentioning clearly but what I meant was the subclass returned from the ArrayList.subList (int, int) method and not the abstractList itself. For some reason I thought it was implied but anyways. There's no add method in the SubList inner class of ArrayList, Roel:
G Batra wrote:Case 1: Simple case when add(E) is invoked on the subList reference. The element gets added at the end of the list in both the lists. Works fine. No RTE thrown. Behavior however, is inconsistent with NavigableSet and Map. But as Roel pointed out it is how the method was implemented.
G Batra wrote:Case 2: When add(E) is called on the backed list. RTE ConcurrentModificationException thrown. Why? The method call was just an attempt to add an element at the end of the list. Looks like this operation is independent of any impact on the sublist. Where's the concurrentModification?
G Batra wrote:And Roel, if I try to add an element at position 5 on the sublist despite the list size of being 4, it throws RTE IndexOutOfBoundsException.
G Batra wrote:
Case 2: When add(E) is called on the backed list. RTE ConcurrentModificationException thrown. Why? The method call was just an attempt to add an element at the end of the list. Looks like this operation is independent of any impact on the sublist. Where's the concurrentModification? The excerpt from the Javadoc of subList method is vague:
"..... The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list......."
G Batra wrote:After carefully going through your post at least twice it has started to make some sense. Thank you for taking time out and putting effort in such an extraordinary post.
Exceptional explanation.
Thank you again for resolving yet another post.