-2

So i want to remove the Integer from the ArrayList that i already have put into the normal Array.

What i have right now is:

 int[] playersArray = null;

 ArrayList<Integer> spelers = new ArrayList<Integer>();

the table.length u see under here is the amount of tables there are, the table[j].length is the amount of players each table can get. It works good but it removes the wrong Integer from the PlayerList, it removes the next one, not the one thats put into the playersArray.

 for(int j = 0; j < table.length; j++){
     playersArray = new int[table[j].length];
     for(int i = 0; i < table[j].length; i++){
         playersArray[i] = playersList.get(i);
         playersList.remove(i);
     }
     table[j] = playersArray;
     System.out.println("tafel: " + (j+1) + " " +   Arrays.toString(tafel[j]));
 }
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
  • Have you tried to read what `ArrayList#remove` does and how the content of the list behaves? Just use one of the methods from [here](http://stackoverflow.com/questions/718554/how-to-convert-an-arraylist-containing-integers-to-primitive-int-array) and clear `playersList` after that ... – Tom Jun 15 '16 at 23:02
  • Show the declaration of `table` as well. – Jim Garrison Jun 15 '16 at 23:08

3 Answers3

1

Look at your loop:

for(int i = 0; i < table[j].length; i++){
  playersArray[i] = playersList.get(i);
  playersList.remove(i);
}

At i == 0 you remove item 0 from playersList. This moves all subsequent elements down one, item 1 to 0, item 2 to 1, and so on. Then you increment i to 1. Now you get(i) which yields item 1, but that is the new item 1. The old item 1 has been moved to item 0! You are retrieving the old item 2 at this point.

This is an example of why you should not structurally change a collection whilst iterating over it.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
1

From the documentation of ArrayList.remove:

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

So if your playersList is like this

playersList[0] = 1
playersList[1] = 2
playersList[2] = 3
playersList[3] = 4

after playersList.remove(0) the list will be

playersList[0] = 2
playersList[1] = 3
playersList[2] = 4

So the next operation (playersList.remove(1)) will not be executed on the correct element.

If you have just to loop over all the elements you could just substitute playersList.remove(i) with playersList.remove(0), and the same goes for playersList.get(i).

Loris Securo
  • 7,538
  • 2
  • 17
  • 28
0

As others have explained, you're collapsing the list as you remove each element, causing your iteration to skip elements. Here are several alternatives:

1) Keep pulling out the first element:

for(int i = 0; i < table[j].length; i++){
    playersArray[i] = playersList.remove(0);
}

2) Remove while iterating using Iterator.remove():

Iterator<Integer> playersIter = playersList.iterator();
for(int j = 0; j < table.length; j++){
    ...
    for(int i = 0; i < table[j].length; i++){
        playersArray[i] = playersIter.next();
        playersIter.remove();
    }
    ...
}

3) If you're not expecting or don't need leftover elements, you can get each element by its index and clear the list when you're done:

for(int j = 0; j < table.length; j++){
    ...
    for(int i = 0; i < table[j].length; i++){
        playersArray[i] = playersList.get(i);
    }
    ...
}
playersList.clear();

All of these solutions assume playersList has sufficient elements to fill table, as you do in your example.

shmosel
  • 49,289
  • 6
  • 73
  • 138