[編輯]相當新的網站,以便試圖澄清問題。刪除方法中的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.
我怎樣才能讓這個當我刪除從一個元素數組在我的匹配方法,它實際上會從我的主要方法中的數組中刪除該元素?這甚至有可能嗎?如果可能的話,我寧願有適合我的代碼的東西,而且我不想實現另一種方法,如果我不需要,因爲實際上比這更多,但是這給了什麼我想要發生。
,你能不能給我們一個[MCVE] – Gendarme
請注意,數組和ArrayList是兩個不同的東西。 – Gendarme
'exampleArray.remove(1);'應該可能是'exampleArray.remove(elem1);' –