2014-01-13 27 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 –