I tried to build my first Android app (a noobish Snake remake) and I noticed the program always crashes once my Snake reaches a length of 3.
Every part of my snake is stored in a class called "Segment", which stores the x and y coordinates and the lifetime of this bodypart.
static List<Segment> list = new ArrayList<Segment>();{ //my list
Segment startingSegment = new Segment(0, 0, 1); //adding starting element to my list
list.add(startingSegment);
}
For test reasons I made a special loop which adds elements to my list:
while(list.size() < 2){ //this works
Segment newSegment = new Segment(0, 0, 1);
list.add(newSegment);
}
while(list.size() < 3){ //this crashes the app already
Segment newSegment = new Segment(0, 0, 1);
list.add(newSegment);
}
Logcat:
04-05 15:50:19.921: E/AndroidRuntime(14308): FATAL EXCEPTION: Thread-3556 04-05
15:50:19.921: E/AndroidRuntime(14308): Process: com.example.snake, PID: 14308 04-05
15:50:19.921: E/AndroidRuntime(14308): java.util.ConcurrentModificationException 04-05
15:50:19.921: E/AndroidRuntime(14308): at
java.example.ArrayList$ArrayListIterator.next(ArrayList.java:573)
I really don't know what's going on there and would appreciate some help.