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;
}
}
}