ArrayList
in
Programming Questions
•
2 years ago
I am stuck on this bit of code. What I am trying to do is very simple.
I am trying to generate a list of integers in the ArrayList, then select 2 of those integers and place them in a new array.
then remove those values form the ArrayList.
For some reason I keep getting IndexOutOfBoundsException errors. I have been over the code many times now I can't see what is wrong.
Can someone help? Thanks in advance for your advice.
- import java.util.ArrayList;
- ArrayList<Integer> balls;
- ArrayList<Integer> keeps;
- int[][] matrix = new int[100][2];
- balls = new ArrayList<Integer>();
- keeps = new ArrayList<Integer>();
- balls.add(6);
- //make list of int values
- for(int i = 0; i <10; i++){
- for(int j = 0; j <3; j++){
- balls.add(i);
- }
- }
- //pick two random values from the array list and add them to the array
- for(int i = 0; i < 15; i++){
- int ranA = balls.get((int)random(0,balls.size()));
- int ranB = balls.get((int)random(0,balls.size()));
- println(balls.size());
- matrix[i][0] = balls.get(ranA);
- matrix[i][1] = balls.get(ranB);
- balls.remove(ranA);
- if(ranA < ranB){
- balls.remove(ranB-1);
- }
- else{
- balls.remove(ranB);
- }
- }
- //print results
- for(int i = 0; i <30/2; i++){
- println(matrix[i][0] + "," + matrix[i][1]);
- }
1