2014-01-13 31 views
0

我試圖替換我的字符串arraylist中的值,但我的arraylist只是似乎添加而不是替換。ArrayList被填充而不是替換索引java

temp是一個從緩衝區讀取器獲取值的字符串。 player_status_line是我的字符串arraylist。

我看不出我做錯了什麼。我檢查數組大小,如果它包含用戶名(getName [2]),我將新值設置爲相同的位置。

這是我所做的,但不知何故,它仍然保存了一切。試圖通過代碼,但看不到問題。

代碼編輯*

while ((checkIfPlayer = bReader.readLine()) != null) { 
    // Arraylist 
    String temp = checkIfPlayer.trim(); 
    System.out.println(temp); 
    if (temp.contains("ZOMBIE") || temp.contains("HUMAN")) { 
     String[] getName = temp.split(" "); 
     String checkName = getName[2]; 
     if (!player_status_list.contains(checkName)) { 
      player_status_list.add(temp); 
     } else if (player_status_list.contains(checkName)) { 
      for (int i = 0; i < player_status_list.size(); i++) { 
       player_status_list.set(i, temp); 
      } 
     } 
     DrawTheMap(); 
    } 
} 
+2

如果我是你,我會放在一起,演示行爲的小測試案例。只是一個使用硬編碼值和預定義列表做類似事情的主要方法。這樣我們就可以爲自己運行代碼,看看你在說什麼。 –

+0

作爲一個例子,'temp'實際上在字符串中保存了什麼? – gtgaxiola

+0

編輯我的答案以迴應您的代碼更改。 – gtgaxiola

回答

0

您的支票是不正確的:

if (!player_status_list.contains(checkName)) { 
    player_status_list.add(temp); 
} else if (player_status_list.contains(checkName)) { 
    for (int i = 0; i < player_status_list.size(); i++) { 
     player_status_list.set(i, temp); 
    } 
} 

這裏是嵌入的註釋細分:

//temp = "4711 PLAYER joseph HUMAN 30.1 30.1"; 
//checkName = "joseph" 

//This is checking if the list contains: "joseph" 
if (!player_status_list.contains(checkName)) { 
    //If it doesn't have it we are adding: "4711 PLAYER joseph HUMAN 30.1 30.1"; 
    //This doesn't seem right!!! 
    player_status_list.add(temp); 

    //This will never happen because list is always adding the temp variable not the name 
} else if (player_status_list.contains(checkName)) { 
    for (int i = 0; i < player_status_list.size(); i++) { 
     player_status_list.set(i, temp); 
    } 
} 

嘗試以下操作:

while ((checkIfPlayer = bReader.readLine()) != null) { 
    // Arraylist 
    String temp = checkIfPlayer.trim(); 
    System.out.println(temp); 
    if (temp.contains("ZOMBIE") || temp.contains("HUMAN")) { 
     String[] getName = temp.split(" "); 
     String checkName = getName[2]; 
     boolean added = false; 
     for(int i = 0; i < player_status_list.size(); i++) { 
      if(player_status_list.get(i).contains(checkName)) { 
       player_status_list.set(i, temp); 
       added = true; 
      } 
     } 
     if(!added) { 
      player_status_list.add(temp); 
     } 
     DrawTheMap(); 
    } 
} 

我測試的輸入(當然我註釋掉DrawTheMap()部分)

ASYNC PLAYER joseph HUMAN 30.0 30.0 
ASYNC PLAYER joseph HUMAN 50.0 80.0 
ASYNC PLAYER BOB ZOMBIE 30.0 30.0 
ASYNC PLAYER GIL ZOMBIE 30.0 30.0 

這是導致player_status_list我得到:

ASYNC PLAYER joseph HUMAN 50.0 80.0 
ASYNC PLAYER BOB ZOMBIE 30.0 30.0 
ASYNC PLAYER GIL ZOMBIE 30.0 30.0 
+0

所以我改變了代碼: –

+0

這有幫助嗎? – gtgaxiola

+0

我忘了編輯我的代碼。現在做了嗎,看不到我做錯了什麼,因爲它不斷添加到數組列表中。 –

0

在你的循環中,首先檢查,看如果player_status_list.get(i)== checkName,那麼只需使用checkName替換該索引處的變量即可。

您也可以通過做簡化您發佈的整個代碼:

if !containsname 
    add name 
else 
    find index at the name (maybe use forloop) 
    replace at index with name 
+0

嘿,我相信我改變了你說的方式比較簡單,但它仍然在arraylist中添加更多和mroe。一直在做這個android應用程序8小時,所以我的頭有點失去工作:P –