Quantcast
Channel: Initialization of an ArrayList in one line - Stack Overflow
Viewing all articles
Browse latest Browse all 37

Answer by Charif DZ for Initialization of an ArrayList in one line

$
0
0

The best way to do it:

package main_package;import java.util.ArrayList;public class Stackkkk {    public static void main(String[] args) {        ArrayList<Object> list = new ArrayList<Object>();        add(list, "1", "2", "3", "4", "5", "6");        System.out.println("I added " + list.size() +" element in one line");    }    public static void add(ArrayList<Object> list, Object... objects) {        for (Object object : objects)            list.add(object);    }}

Just create a function that can have as many elements as you want and call it to add them in one line.


Viewing all articles
Browse latest Browse all 37

Trending Articles