Search code examples
javaarraysarraylistargs

Using "args" but method still not taking more than one array as parameter?


I want to make a method that takes x amount of ArrayList<> as a parameter. When I use args this does not work. Is args only reserved for int and String etc? Why does not args become a new array containing my arraylist? I thought there was a way to be able to give multiple parameter values to a method that turns into an array inside the method?

ArrayList<String> list1 = new ArraytList();
list1.add("1");
ArrayList<String> list2 = new ArrayList();
list2.add("2");
list2.add("3");

A print method:

public void print(ArrayList<String> args){
    for(int i = 0; i < args.length; i++){
        for(int j = 0; j < args[i].size(); j ++){
            System.out.println(args[i].get(j));
        }
    }
}

Does not work


Solution

  • printElements takes a single parameter of type ArrayList<String>, but you're trying to give it two arguments of type ArrayList.