2016-06-13 24 views
1

[編輯]相當新的網站,以便試圖澄清問題。刪除方法中的ArraList元素

我正在一個項目中,我有一個方法,其中一個參數是一個數組。

實施例:

public class Match 

public void playMatch(int teamA, int teamB, ArrayList<String> groups) 
Random scoreA = new Random(); 
int score1 = scoreA.nextInt(5) + 0; 
int score2 = scoreA.nextInt(5) + 0; 
System.out.println("Qtr 1: " + score1 + " " + score2); 

if (score1 > score2){ 
    System.out.println(groups.get(teamA) + " win " + score1 + " to + 
    score2 + " " + teams.get(teamB) + " eliminated."); 
      teams.remove(teamB); 
} 
else if (score2 > score1){ 
    System.out.println(groups.get(teamB) + " win " + score2 + " to " + 
    score11 + " " + teams.get(teamA) + " eliminated."); 
      teams.remove(teamA);} 
} 
public static void main(String[] args) { 
Match game1 = new Match(); 
ArrayList<String> groups = new ArrayList<String>(
      Arrays.asList("team1", "team2", "team3")); 
System.out.println("Round 1"); 
game1.playMatch(0, 1, groups); 

的問題是,當我刪除元件在我匹配方法不從在我的主要方法ArrayList中移除。這是一個問題,因爲我希望能夠然後做:

game2.playMatch(0, 1, groups)

其中1 = team3而不是團隊2.

我怎樣才能讓這個當我刪除從一個元素數組在我的匹配方法,它實際上會從我的主要方法中的數組中刪除該元素?這甚至有可能嗎?如果可能的話,我寧願有適合我的代碼的東西,而且我不想實現另一種方法,如果我不需要,因爲實際上比這更多,但是這給了什麼我想要發生。

+2

,你能不能給我們一個[MCVE] – Gendarme

+0

請注意,數組和ArrayList是兩個不同的東西。 – Gendarme

+0

'exampleArray.remove(1);'應該可能是'exampleArray.remove(elem1);' –

回答

0

我重構了您給出的內容,下面的內容適用於我的目的,並且似乎是按照您的要求做的。修復了一堆語法錯誤和格式化的東西。看起來你沒有考慮關係score1 == score2。請嘗試下面的代碼,以我添加到您的主法修改前後的名單打印出來的筆記,看看它是否有助於

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import java.util.Random; 

public class MyMatch { 

    public void playMatch(int teamA, int teamB, List<String> groups) { 
     Random rand = new Random(); 
     int score1 = rand.nextInt(5) + 0; 
     int score2 = rand.nextInt(5) + 0; 
     System.out.println("Qtr 1: " + score1 + " " + score2); 

     if (score1 > score2){ 
      System.out.println(groups.get(teamA) + " win " + score1 + " to " + 
      score2 + " " + groups.get(teamB) + " eliminated."); 
      groups.remove(teamB); 
     } else if (score2 > score1){ 
      System.out.println(groups.get(teamB) + " win " + score2 + " to " + 
      score1 + " " + groups.get(teamA) + " eliminated."); 
      groups.remove(teamA); 
     } else if (score1 == score2) { 
      System.out.println(groups.get(teamA) + " tie " + score1 + " to " + 
      groups.get(teamB) + " " + score2 + " no team was eliminated."); 
     } 
    } 

    public static void main(String[] args) { 
     MyMatch game1 = new MyMatch(); 
     List<String> groups = new ArrayList<String>(Arrays.asList("team1","team2", "team3")); 
     System.out.println("Round 1"); 
     System.out.println("BEFORE: " + groups); 
     game1.playMatch(0, 1, groups); 
     System.out.println("AFTER: " + groups); 
    } 
} 
+0

這工作,爲什麼這只是從ArrayList語法切換到列表雖然?我的意思是,這實際上是我通過這個答案所做的,並且它在ArrayList沒有的地方起作用。 – Smackelbap

+0

這不是現在它工作的原因。這是另一篇關於多態性的長篇討論,對於這篇文章來說太長了。這是一個很好的鏈接。 http://www.tutorialspoint.com/java/java_polymorphism.htm。無論如何,你的代碼中有很多語法錯誤(比如缺少花括號,錯誤地命名像score11和團隊的變量,缺少第一個打印標記中的引號等等),更不用說格式化了,所以很難閱讀。你是否在使用類似eclipse的IDE?如果是這樣,它會捕獲代碼中的編譯錯誤。除此之外,你的代碼很好! –

+0

ArrayList是一個列表。 Java(JVM)計算出它在運行時List的哪個實現(ArrayList,LinkedList等)。如果在方法中使用List 作爲參數類型而不是ArrayList ,這是一個最佳實踐,因爲它使您的代碼能夠靈活地接受List的不同實現,從而使您的代碼更具可伸縮性,並且可以在以後重用。你也可以用List來實例化你的List,而不是ArrayList作爲你聲明的類型(對象賦值的左邊)。合理? –

0

在下面的例子中,您將傳遞removeByIndex方法的三個參數:a(給定索引),b(另一個給定索引)和arrayList(元素的容器)。然後,它將創建容器的副本(克隆),在a和b中找到最低索引,從arrayList中刪除該索引處的元素,並返回修改後的副本。

public static ArrayList<String> removeByIndex(int a, int b, ArrayList<String> arrayList) { 

    ArrayList<String> clone = (ArrayList<String>) arrayList.clone(); 

    int minIndex = Math.min(a, b); 

    clone.remove(minIndex); 

    return clone; 
} 

public static void main(String[] args) { 

    ArrayList<String> example = new ArrayList<String>(); 

    example.add("One"); 
    example.add("Two"); 
    example.add("Three"); 
    example.add("Four"); 
    example.add("Five"); 

    System.out.println(example); 

    example = removeByIndex(1, 3, example); 

    System.out.println(example); 
} 

請注意,您必須將該方法的返回值指定給容器,否則將不會被修改。

+0

我對這個可憐的問題格式表示抱歉,我對這個網站相當陌生。我希望這更清楚。 – Smackelbap

+0

@Marcelo Vinicius:嗯....你能幫我理解爲什麼你要克隆arrayList嗎? –

+0

將克隆技術轉化爲一種方法不是一回事嗎?我正在這樣做,以便我可以調用該方法。如果我有50個團隊,我可以在初始化ArrayList並用團隊填充之後調用該方法50次,而不是寫入數百行代碼。這是你問的嗎? – Smackelbap