/** * @author lautturi.com * Java example: put user input into arraylist in java using Scanner */ import java.util.*; public class Lautturi { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("enter elements"); ArrayList<Integer> LIST = new ArrayList<Integer>(); while (sc.hasNextInt()) { int i = sc.nextInt(); LIST.add(i); } System.out.println(LIST); } }
output:
enter elements 1 2 3 4 5 a [1, 2, 3, 4, 5]