java - Variable number of list (ArrayList) as an argument to method -
there way in java handle variable number of string arguments like
public void returnstring(string...args){ //some code } // calling returnstring("abc","def","klm")
i wanted know if same can applied collections.
if not, alternative. read array of list not possible, wondering how might work.
//sample code of trying public void returnlist(list...args){ //some code }
i don't know problem.... here 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. insert 2 lists.
access 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) (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. insert 2 lists. insert 3 lists. first value. second value. third value.
Comments
Post a Comment