2015-07-10 84 views
0

我有一個目的插入值的數組列表,並刪除任何重複

Table[] tables = new Table[10]; 

此列表存儲信息約10球員和他們的位置從頂1,其爲表[推移的數組列表0]和前10名等於表[9]

然而,有時我需要在數組之間放置一個值,並且讓我們說我輸入的球員名稱必須是前5名,也就是說,top 5進入前4,從前4到3 ..和3到2 ..等等,但我也需要檢查這些值是否包含我剛剛添加的球員名稱,所以在這種情況下,我必須將其刪除,並將所有列表備份,很多添加1,有沒有人有最好的方式來做到這一點,我想這樣做的方式可能不是最好的。

這裏是我做了那麼遠,但它不是完整的..

private void addToBoard(Player damaged, Player killer) { 
    if(damaged.getName().equalsIgnoreCase(killer.getName())){ 
     return; 
    } 
    for(Table table : tables){ 
     if(table != null){ 
      if(table.currentPlayer.equalsIgnoreCase("No One")){ 
       table.currentPlayer = killer.getName(); 
       break; 
      }else if(table.currentPlayer.equalsIgnoreCase(damaged.getName()) || table.currentPlayer.equalsIgnoreCase("Searching...")){    
       if(table.currentPlayer.equalsIgnoreCase(killer.getName())){ 
        return; 
       } 
       if(table.currentPlayer.equalsIgnoreCase(killer.getName())){ 
        return; 
        //update the list, and remove duplications 
       } 
       if(!table.currentPlayer.equalsIgnoreCase("Searching...")){ 
        killer.chat("I killed "+damaged.getName()+" and now I am Top: "+table.topID+" gf :)"); 
       } 
       table.currentPlayer = killer.getName(); 
       if(table.topID != 10){ 
        //make a list on a hashmap with the key from 1 to 10 
        HashMap<Integer, Table> addAll = new HashMap<Integer, Table>(); 
        //add the top 
        for(int i = 0; i < tables.length; i++){ 
         addAll.put(tables[i].topID, tables[i]); 
        } 
        HashMap<Integer, Table> updated = new HashMap<Integer, Table>(); 
        String oldPlayer; 
        for(Entry<Integer, Table> top : addAll.entrySet()){ 
         if(top.getValue().currentPlayer.equalsIgnoreCase(damaged.getName())){ 
          //dont add 
          oldPlayer = top.getValue().currentPlayer; 
          top.getValue().currentPlayer = killer.getName(); 
          Table next = updated.get((top.getValue().topID+1)); 
          next.currentPlayer = oldPlayer; 
          updated.put(next.topID, next); 
         }else{ 
          if(updated.containsKey(top.getValue().topID)){ 
           updated.put((top.getValue().topID+1), top.getValue()); 
          }else{ 
           updated.put(top.getValue().topID, top.getValue()); 
          } 
         } 
        } 
        for(int i = 0; i < tables.length; i++){ 
         if(updated.get(i) != null){ 
          tables[i] = updated.get(i); 
         } 
        } 
       } 
       break; 
      } 
     } 
    } 

} 

的感謝!

+0

我正在使用java。 – Stefan15ist

+0

顯示你試圖解決你的問題的一個例子 – UnknownOctopus

+0

呃..我試圖做到這一點,但後來我迷失了哈哈..這裏是:http://hastebin.com/vofiqisore.avrasm – Stefan15ist

回答

0

請勿使用數組。改爲使用List,其支持myList.add("New Guy", 4) - 即將玩家添加到給定位置,並將所有其他玩家移動一步。然後,您可以用myList.remove(10)取出11日(舊10日):

myList.add("New Guy", 4); 
myList.remove(10); 

你可以做陣列同樣的事情,但它的可讀性:

// copies 4 elements from (old) pos. 4 to its new pos. 5 
System.arraycopy(tables, 4, tables, 5, 4); 
tables[4] = "New Guy"; 

或者使用一個循環,這既是難以閱讀(它必須向下以避免丟失信息)和更詳細:

for (int i=9; i>=5; i--) tables[i] = tables[i-1]; 
tables[4] = "New Guy"; 
+0

非常感謝,這真的很有用 – Stefan15ist