mardi 14 juin 2016

Removing ArraList Elements in a Method

[EDIT]Fairly new to the site so editted to try to clarify the problem.

I am working on a project where I have a method in which one of the parameters is an array.

Example:

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);

The problem is that when I remove the element in my Matches method it does not remove it from the ArrayList in my main method. This is an issue because I want to be able to then do:

game2.playMatch(0, 1, groups)

Where 1 = team3 instead of team 2.

How can I make it so that when I remove an element from the Array in my Matches method it actually will remove that element from the Array in my main method? Is this even possible? I would prefer to have something that fits into my code if it is possible and i don't want to have to implement another method if I don't have to as there is actually a lot more than this but this gives the jist of what I want to have happen.

Aucun commentaire:

Enregistrer un commentaire