Smackelbap Smackelbap - 5 months ago 7
Java Question

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.

Answer

I refactored what you gave and the below works on my end and seems to do what you're asking. Fixed a bunch of syntax errors, and formatting stuff. Looks like you're not accounting for ties where score1 == score2. Please try the below code, taking note of the print outs of the list I added to your main method before and after modification, and see if it helps

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