如果我創建一個大小爲5的新的ArrayList ...添加到一個空的ArrayList的中間?
Player P;
ArrayList<Player> orderedPlayers = new ArrayList<Player>(5);
,然後我嘗試添加到ArrayList的中間...
orderedPlayers.add(2, P);
我得到一個indexoutofbounds ...我也得到indexoutofbounds如果我使用的設置,而不是添加...
orderedPlayers.set(2, P);
其實我可以添加P到ArrayList中的唯一方法是,如果我使用的0指數...
orderedPlayers.add(0, P);
也出於一些奇怪的原因,當我這樣做時,我的調試器在eclipse中看到該元素添加到orderedPlayers的第4個索引而不是第0個...是ArrayList越野車還是我完全失去了一些東西?我將如何添加到空ArrayList的中間?
可能的重複http://stackoverflow.com/questions/8896758/initial-size-for-the-arraylist – Raedwald 2016-02-08 15:29:40
可能重複的[ArrayList初始容量和IndexOutOfBoundsException](http://stackoverflow.com/questions/11908037/arraylist-initial-capacity-and-indexoutofboundsexception) – Raedwald 2016-02-08 15:31:09