0

Alright so I am working on a small program in which I have a method which accesses an Arraylist.

So:

public void setGroups(int groupA, int groupB, ArrayList< String > groups)

then I have my Arraylist in the body of my main method:

ArrayList groupSets = new ArrayList< String >( Arrays.asList("group1", "group2" ));

So my question is, how do I call this code in my main method? My issue is with the arraylist part. Also these ints of groupA/B, I will be using these to pull elements out of the ArrayList.

So it would be like?:

playGame(0, 1, ArrayList< String > groupSets);

Except I know that the arraylist part is wrong and I am unsure if the ints are right or wrong as well, they seem right but I could be completely off. Please any help?!

1
  • 1
    Why not make that method return an Arraylist? Having a setter method with side effects seems to be a bad design Commented Jun 13, 2016 at 2:08

3 Answers 3

1

Your declaration and call are different, so your question is a bit confusing.

The call should not include any type information:

setGroups(groupA, groupB, groups);
Sign up to request clarification or add additional context in comments.

2 Comments

Alright so when I format my call in this fashion: setGroups(0, 1, groups); it throws an error stating "non-static method setGroups(int, int, ArrayList<String>) cannot be referenced from a static context.
Create an instance of the class that implements that method.
0

Its just

ArrayList<String> groupSets = new ArrayList<String>(); 
playGame(0, 1, groupSets);

Comments

0

In agreement with both of the partial answers above, let's see if we can make this comprehensive:

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

public class GroupStuff {

    public static void main(String[] args) {
        List<String> groupSets = new ArrayList<>(Arrays.asList("group1", "group2"));
        GroupStuff gs = new GroupStuff();
        gs.setGroups(0,1,groupSets);
    }

    public void setGroups(int groupA, int groupB, List<String> groups) {    
        //Do whatever you do here...
    }
}

And....

playGame(0, 1, ArrayList< String > groupSets);

should be

playGame(0, 1, groupSets);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.