So today I learned you can't modify an arraylist while you are iterating through it. In this case, I must be able to add elements though, while staying inside my loop.
Any ideas? I tried using the Iterator class based off some examples online.
Excuse my in-elegant code, it was something It's a spur-of-the-moment assignment.

import java.util.ArrayList;
import java.util.Iterator;


public class MiceCalculator {
	ArrayList<Mouse>mouseList = new ArrayList<Mouse>();
	int mousecount = 0;
	public static void main(String[] args) {
		MiceCalculator calc = new MiceCalculator();
		calc.run();
	}
	public void run()
	{		
		
	mouseList.add(new Mouse(true, true, 0, 6, 0));
	mouseList.add(new Mouse(false, true, 0, 0, 0));
	mouseList.add(new Mouse(true, true, 0, 0, 0));
	mouseList.add(new Mouse(true, true, 0, 0, 0));
	mouseList.add(new Mouse(true, true, 0, 0, 0));

		int daysRan = 0;
		
		while(daysRan<=151)
		{
			for (Iterator<Mouse> it = mouseList.iterator(); it.hasNext();) {
			    Mouse m = it.next();

				if(m.isFemale && m.isAdult)
				{
					if(m.daysSinceLitter==40)
					{
						m.daysSinceLitter=0;
						addLitter();
					}
				}
				
				if(m.isFemale && m.isAdult!=true)
				{
					if(m.daysSinceLitter==120)
					{
						m.isAdult = true;
						m.daysSinceLitter=0;
						addLitter();
					}
				}
				m.daysSinceLitter++;
			}
			daysRan++;

		}
		System.out.println(mousecount);
	}
	public void addLitter()
	{
		System.out.println("here");
		for(int x = 0;x<=3;x++)
		{
		mouseList.add(new Mouse());
		mousecount++;
		}
	}
	public void addDay()
	{
		for(Mouse m : mouseList)
		{
			m.daysOld ++;
			m.daysSinceLitter++;
		}
	}
	
	public class Mouse
	{
		boolean isFemale = false;
		boolean isAdult = false;
		int daysSinceLitter = 0;
		int totallitterProduced = 0;
		int daysOld = 0;
	    
		public Mouse()
		{
		}
		
		public Mouse(boolean isaFemale, boolean isAnAdult, int DaysSinceLitter, int totalLitterProduced, int daysold)
		{
			isFemale = isaFemale;
			isAdult = isAnAdult;
			daysSinceLitter = DaysSinceLitter;
			totallitterProduced = totalLitterProduced;
			daysOld = daysold;
		}

	}
}

Recommended Answers

All 5 Replies

Instead of an iterator, you can use the following for loop:

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

and get reference of current Object, using

Mouse m = mouseList.get(i);

But I dont know what ure trying to do. This will allow u to modify the array list.
And I dont know what implications this will have on ur algorithm. You need to do some testing for that. Good luck!

... or, since this is 2011, you can use the enhanced for loop from Java 1.5

A common solution to this problem is to make a copy of the original list and iterate thru that while updating the original.

ArrayList<Mouse> master = .....
ArrayList<Mouse> copy = new ArrayList<Mouse>(master); // copies references from master
for (Mouse m : copy) {
  // access Mouse m, do anything with master
}

Oh Yeah! Right on James! Forgot the for each loop....

If you want to modify a `List` while iterating on it, the simplest solution would be to use a list iterator as opposed to a regular iterator. List iterators allow destructive updates on the list while iterating over it.

public static void main(String[] args) throws Exception {
	List<String> lst = new LinkedList<String>(Arrays.asList("a,b,c,d".split(",")));
	for (ListIterator<String> iter = lst.listIterator(); iter.hasNext(); ) {
		if (iter.next().equals("b")) {
			iter.add("OMG_THE_MIDDLE!!11!");
		}
	}
	System.out.println(lst);
}

There's always something new to learn in the Java API! Thank you for that ~s.o.s~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.