2012-04-13 197 views
1

固定!感謝大家!從Arraylist分割字符串(java)

好了,所以我不知道爲什麼,這是給我一個ArrayIndexOutOfBoundsException ..

人是一個ArrayList保存數值如:

2,6,m 
1,7,w 
5,3,c 
0,4,w 
1,6,w 
2,3,w 
1,5,w 

這裏就是我試圖讓每個數組列表索引,將其分爲:行,列,字母。 這是說temp [2]是outofbounds。爲什麼?我的意思是拆分方法應該使3部分,0,1和2.那麼爲什麼2給我適合。我跑了一些測試,它也給了temp [0]和temp [1]調用的outofbounds。

for (int i = 0; i < al.size(); i++) { 
     String[] temp = al.get(i).toString().split(","); 
     System.out.println(temp.length); 
     System.out.println(temp[1]); 
     if (temp[2].equals("m")) { 
      boardTiles[Integer.parseInt(temp[0])][Integer.parseInt(temp[1])].setIcon(mousetile); 
      boardTiles[Integer.parseInt(temp[0])][Integer.parseInt(temp[1])].putClientProperty("mouse", true); 
     } 
     if (temp[2].equals("c")) { 
      boardTiles[Integer.parseInt(temp[0])][Integer.parseInt(temp[1])].setIcon(cheesetile); 
      boardTiles[Integer.parseInt(temp[0])][Integer.parseInt(temp[1])].putClientProperty("cheese", true); 
     } 
     if (temp[2].equals("w")) { 
      boardTiles[Integer.parseInt(temp[0])][Integer.parseInt(temp[1])].setIcon(metalwall); 
      boardTiles[Integer.parseInt(temp[0])][Integer.parseInt(temp[1])].putClientProperty("wall", true); 
     } 
    } 

我是否利用String []與split方法錯誤或什麼? 謝謝, -Austin

EDITS:

我只是跑了關於ArrayList中循環,所有的值都在那裏。

boardTiles [x] [x]是一個JButton的二維數組。

異常是發生在索引:1.

的。長度被表示爲1,以及。

+1

請顯示'boardTiles'的定義 – 2012-04-13 15:50:05

+0

索引是什麼異常發生在? – 2012-04-13 15:50:07

+1

你確定你的輸入總是像你上面給出的示例輸入嗎?嘗試每次打印輸入以查看哪個輸入會給您提供錯誤。 – Danny 2012-04-13 15:50:26

回答

2

boardTiles可能沒有在您的輸入數組中定義的某些值的大小。

+0

@jeurgend不能,在我做這部分之前,它運行一個boardbuilder方法,它在JFrame中顯示得很好。所以這不是問題。 – Austin 2012-04-13 15:57:31

0

做一個al.get(i)的打印並仔細檢查它確實是逗號分隔的一串值。