7 Replies - 840 Views - Last Post: 03 August 2013 - 07:29 AM Rate Topic: -----

#1 iridebmxnj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 15-December 12

Remove even integers from linked list

Posted 31 July 2013 - 09:52 AM

In this program I am creating an array and then changing it into a linked list, after changing it into a linked list I want to remove all the even integers.
Somewhere along the line I am going wrong and getting this error.

Can someone point me in the right direction thanks.


Exception in thread "main" [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at arrayToList.main(arrayToList.java:34)


 

import java.util.*;




public class arrayToList {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		//Array
		Integer[] stuff = new Integer[20];
		
		//Creates list from 1 to 20
		for(int x = 0; x < stuff.length; x++)
		{
			
			stuff[x] = x + 1;
	
		}
		
		
		//Prints out array
		System.out.println(Arrays.toString(stuff));
		//Change array to linkedList 
		LinkedList<Integer> theList = new LinkedList<Integer>(Arrays.asList(stuff));
		
		
		//Remove all of the even integers in list 
		for(Integer item: theList)
		{
			if(item % 2 == 0)
			{
				
				theList.remove(item);
				
			}
			
			
		}
		
		
		//Prints out new list with no even integers 
		System.out.println(theList);
		
		
		
		
		
		
	}//end main 

}//end class




Is This A Good Question/Topic? 0
  • +

Replies To: Remove even integers from linked list

#2 iridebmxnj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 15-December 12

Re: Remove even integers from linked list

Posted 31 July 2013 - 10:03 AM

Problem solved!


for(int i = 0; i < theList.size(); i++)
		{
			
			System.out.println(theList.get(i));
			
			if(theList.get(i) % 2 == 0)
			{
				theList.remove(i);
			}
			
		}



Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1946
  • View blog
  • Posts: 4,047
  • Joined: 11-December 07

Re: Remove even integers from linked list

Posted 31 July 2013 - 10:23 AM

Are you sure? What happens if you use the array {2, 4, 6, 8, 10, 12}?
Was This Post Helpful? 0
  • +
  • -

#4 iridebmxnj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 15-December 12

Re: Remove even integers from linked list

Posted 02 August 2013 - 04:49 PM

I thought I was until I ran the program with Integer[] stuff = {2,4,6,8,10,12};
Im not quite sure why this does not work in this instance
Was This Post Helpful? 0
  • +
  • -

#5 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2631
  • View blog
  • Posts: 15,651
  • Joined: 24-September 08

Re: Remove even integers from linked list

Posted 02 August 2013 - 07:04 PM

Create a catch all case for the case in which it was empty, then it wont throw any errors when you try to get and print from the empty list (out of bounds exception I expect?)

EDIT: I can't form words, need sleep, sorry.

This post has been edited by NeoTifa: 02 August 2013 - 07:04 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10437
  • View blog
  • Posts: 38,651
  • Joined: 27-December 08

Re: Remove even integers from linked list

Posted 02 August 2013 - 07:23 PM

When you remove() an element, the size() of the List decreases. So if you remove() an element, you should not be changing the index variable.

I'm guessing the reason you got a ConcurrentModificationException is because you attempted to remove() an element from the List while using an Iterator or a foreach loop (which uses an underlying Iterator). The Iterator interface has its own remove() method you can use, rather than the List remove() method. That will help avoid the problem. Note that with a foreach loop, you cannot access the underlying Iterator.

Quote

Create a catch all case for the case in which it was empty, then it wont throw any errors when you try to get and print from the empty list (out of bounds exception I expect?)

Catching Exceptions should only occur for checked Exceptions, like those associated with the File I/O API. IndexOutOfBoundsExceptions are unchecked Exceptions and should not be handled with a try-catch block.
Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Remove even integers from linked list

Posted 03 August 2013 - 04:23 AM

View Postmacosxnerd101, on 02 August 2013 - 10:23 PM, said:

When you remove() an element, the size() of the List decreases. So if you remove() an element, you should not be changing the index variable.


Process thye List in reverse that simplify the whole thing

   for(int i = theList.size(); --i >= 0;)

instead of

   for(int i = 0; i < theList.size(); i++)


Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2631
  • View blog
  • Posts: 15,651
  • Joined: 24-September 08

Re: Remove even integers from linked list

Posted 03 August 2013 - 07:29 AM

@mac I meant like an if statement or series of if elses, but this is true.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1