• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

subList getting undefined when the backing list is modified

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The API http://download.oracle.com/javase/6/docs/api/java/util/AbstractList.html#subList(int, int) says,
"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. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)".

The subList is created only with the help of the indices mentioned,right? So,whether the subList is modified or the backingList is modified, the indices would remain the same & mustn't the subList be available for us at all times?

Why is that an exception is thrown when the subList is referred, after the backingList being modified in the subList area of it?
(I mean, if a sublist is created with subList(5,10) and if some changes are done to the backing list within indices 5 & 10, the subList becomes invalid)
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means, "what if you move add or remove one of the elements in or before the sublist?"
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinoth Kumar Kannan wrote:Why is that an exception is thrown when the subList is referred, after the backingList being modified in the subList area of it?


It's not clear whether your question is about the design decision (in which case I don't know / don't really care) or the mechanism that throws the exception.

If the latter, look at the code for SubList in AbstractList.java. The current number of modifications of the backing list is cached in the field expectedModCount when the SubList is constructed, and incremented whenever a modification is made via the SubList. Then, when any operation is performed on the SubList, the actual modification count of the backing list is compared to this cached value, and if they differ a ConcurrentModificationException is thrown.

About the design decision, I could hazard a guess that it was considered too expensive to cache all details of every modification and compute whether any interim modification of the backing list would actually make the SubList invalid, so a short cut was adopted that didn't allow any modifications to the backing list post-creation of the SubList. But as already said, I don't really know nor do I worry my head about that. It's adequate that the API documentation reflects the facts unequivocally.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:It's not clear whether your question is about the design decision ....


Yes, its about the design.
Suppose I create a subList,

My doubt is that, if the JVM is going to keep track of the subList with the indices(5 & 10) in the backingList, why is the exception encountered. Here, isn't still the indices still valid, even after the backingList has been modified? Can't it be designed to very well return [6, 7, 8, 9, 10,Vinoth]?
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinoth Kumar Kannan wrote:

Darryl Burke wrote:It's not clear whether your question is about the design decision ....


Yes, its about the design.


Ah. Then we'l have to wait for someone who has some insight or prior knowledge. I've already written all I can guess about the design decision.
 
reply
    Bookmark Topic Watch Topic
  • New Topic