Technology : General Technology Notes: Diary of a late,late adopter.
Updated: 4/27/2004; 10:58:02 AM.

 

Frequent Visits

Subscribe to "Technology" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

Tuesday, April 06, 2004

I wanted to remove a range of items from a list using the List.subList method. I tried the following:
	// al is defined previously as ArrayList al = new ArrayList();
        List l = al.subList(20,50);
        al.removeAll(l);

That got me the java.util.ConcurrentModificationException

The documentation says that a subList is dependent on the List that it is sublisting from. So, while a sublist is open, it is manipulating the parent list and seems to have a lock on it.   Instead, you can create a brand new ArrayList and fill it with the subList, thus freeing up the sublist so that a future call can modify the original List:

	ArrayList sl = new ArrayList(al.subList(10,40));
    	al.removeAll(sl);
		

So why does

 List = myList.subList(10,30);

get you a different animal than

ArrayList temp = new ArrayList(myList.subList(10,30)); ??

because in the first snippet, I haven't instantiated a new object, I just created a reference to an existing object.   In the second snippet, I created a brand new object.  That's what the word "new" gets me.


6:06:45 PM    comment []

© Copyright 2004 mcgyver5.



Click here to visit the Radio UserLand website.

 


April 2004
Sun Mon Tue Wed Thu Fri Sat
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
Mar   May