Saturday, October 28, 2017

ArrayList: add elements at once

This is how to add strings to ArrayList variable.
You can test here: https://www.compilejava.net/

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

public class HelloWorld
{
  public static void main(String[] args)
  {
    ArrayList<String> str = new ArrayList<>(Arrays.asList("test1", "test2", "test3", "test4"));
   
    System.out.print(str.get(0) + ", " + str.get(1) + ", " + str.get(2) + ", " + str.get(3));
  }
}