Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Avoiding concurrent modification exception?

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Location
    UK
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Avoiding concurrent modification exception?

    Hi all.

    I'm wanting to modify an array while it is In use but this throws the exception in the title above. So what is the best way around this? Cloning the array? If I clone the array what is the best way of keeping the clone up to date with each change made?

    What I'm trying to do is add a string and check if the string exists within the array, if it doesn't exists then add it to the same array, but I can not modify the current array which I am iterating through.

    Hopefully I made this clear.

    Any code examples would be greatly Appreciated thanks.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Avoiding concurrent modification exception?

    I can't think of any nice way to handle this other than don't do it.

    You should be able to modify elements safely (assuming you synchronize properly), but if you're adding/removing elements you'll need to obtain new iterators.

    You might want to re-think your program flow. What is it you're trying to accomplish (big picture wise)?

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Location
    UK
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Avoiding concurrent modification exception?

    What I'm trying to do is read through a text file and look for titles then add them to an array just the once. The titles are repeated multiple times in the text file, so I'm trying to just add them to the array once so I have a list of the titles in the order that I find them within the array ready to be output into another text file.

    I have been messing about with code but don't really have anything solid worth posting yet as I'm not really sure of the best way to approach this.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Avoiding concurrent modification exception?

    Hmm, sounds like you're trying to do everything at the same time.

    A better way might be to read in all the titles first, remove all duplicates, and then write out to the file.

    If you have a lot of titles then using an array is not the best way to check for duplicates. It has O(N) duplicate checking.

    Consider using a LinkedHashSet instead. This structure has in theory O(1) duplicate checking and insertion. Duplicates are not added and attempting to add them doesn't change the iterating order.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Location
    UK
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Avoiding concurrent modification exception?

    This seems to work for me so far:
    	private static void  TitleCheck(String n){
    		boolean found = false;
    		String title = null;
    		int pos;
    		pos = n.indexOf(':');
    		if(pos != -1){			
    				title = n.substring(0,pos);
    				if(Titles.size()==0){
    					Titles.add(title);
    				}
     
    				for(String i:Titles){
     
    					if(i.equals(title)){
    						found = true;
    						break;
    					}							
    				}
    			}
     
    		if (found ==false && pos !=-1){
    			Titles.add(title);
     
    		}
    	}

    Have not really tested it properly but delivering the results I was wanting, so far.
    I will have a look at the method you suggested, as this probably is not the best way of doing things. Any other tips/code improvements are welcome, as the way Im doing things might not be the best practice. Thanks for the help so far!

Similar Threads

  1. Replies: 3
    Last Post: February 9th, 2012, 01:04 PM
  2. Blocking in java.util.concurrent Classes
    By bgroenks96 in forum Threads
    Replies: 0
    Last Post: November 14th, 2011, 10:38 PM
  3. avoiding OutOfMemoryException
    By ragavan in forum Exceptions
    Replies: 3
    Last Post: July 27th, 2011, 01:25 PM
  4. concurrent programming
    By abc in forum Threads
    Replies: 10
    Last Post: June 29th, 2010, 08:06 AM
  5. Java Concurrent Programming
    By vzwsudha in forum Threads
    Replies: 6
    Last Post: May 27th, 2010, 05:26 AM