[编辑]相当新的网站,以便试图澄清问题。删除方法中的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);' –