Hello,
I was working through methods of ArrayList class and the method sublist didn't work the way I expected. Need a little help to understand this method's behavior. Two questions in this thread:
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:
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?
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.
Adding supporting code snippet:
Output of above code: