-3

There is a way in Java to handle variable number of String arguments like

public void returnString(String...args){
     //some code
}
// calling it
returnString("abc","def","klm")

I wanted to know if the same can be applied to Collections.
If it does not, what is the alternative. I read that array of List is not possible, so wondering how this might work.

//Sample code of what I am trying to do
public void returnList(List...args){
    //some code
}
c0der
  • 18,467
  • 6
  • 33
  • 65
Abhishek
  • 111
  • 1
  • 3
  • 15
  • 1
    Have you actually tried it? – Ian2thedv Sep 07 '16 at 11:53
  • 1
    The varargs feature is applicable for any type, including collections. Who says it isn't? – Beethoven Sep 07 '16 at 11:57
  • It is called varargs http://docs.oracle.com/javase/7/docs/technotes/guides/language/varargs.html or methods with variable arity http://www.java-tips.org/variable-arity-methods.html – c0der Sep 07 '16 at 11:59

2 Answers2

1

I don't know where is problem.... But here is example:

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

public class Main {

  public static void tryIt(List... lists) {
    System.out.println("You insert " + lists.length + " lists.");
  }
    public static void main(String[] args) {
      List<String> listStrings = new ArrayList<>();
      listStrings.add("TEST");
      List<Integer> listInteger = new ArrayList<>();
      listInteger.add(1);
      tryIt(listStrings);
      tryIt(listStrings, listInteger);
    }
}

Result:

You insert 1 lists.
You insert 2 lists.

Access to arrays example:

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

public class Main {
  public static void tryIt(List... lists) {
    System.out.println("You insert " + lists.length + " lists.");
    if (lists.length>2)
      for (Object obj : lists[2]) {
        System.out.println(obj);
      }
  }
  public static void main(String[] args) {
    List<String> listStrings = new ArrayList<>();
    listStrings.add("TEST");
    List<Integer> listInteger = new ArrayList<>();
    listInteger.add(1);
    List<String> anotherStringList = new ArrayList<>();
    anotherStringList.add("First value.");
    anotherStringList.add("Second value.");
    anotherStringList.add("Third value.");
    tryIt(listStrings);
    tryIt(listStrings, listInteger);
    tryIt(listInteger, listStrings, anotherStringList);
    }
}

Output:

You insert 1 lists.
You insert 2 lists.
You insert 3 lists.
First value.
Second value.
Third value.
Hrabosch
  • 1,541
  • 8
  • 12
1

In JLS §15.12.4.2

If the method being invoked is a variable arity method m, it necessarily has n > 0 formal parameters. The final formal parameter of m necessarily has type T[] for some T, and m is necessarily being invoked with k ≥ 0 actual argument expressions.

So the type will be an array of T. In your first example returnString(String...args), it is String[]. In your second example returnList(List...args), it is List[].

You can of course have arrays of Lists in Java.

Now, if you want in your method body a List instead of an array for args, then the varargs won't help you, but you can build that list with Arrays.asList(args).

Community
  • 1
  • 1