You could use Scanner
:
Scanner scanner = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
while (scanner.hasNextInt())
list.add(scanner.nextInt());
int[] arr = list.toArray(new int[0]);
Until we have closures in java, this is probably the shortest you can get.
int[] arr = list.toArray(new int[0]);
won't work because there's no conversion from Integer to int. You can't use int as a type argument for generics.
But yeah If you are working with Java 8 then you can use Stream API for it with the below code snippet(Better way of doing things).
int[] array = list.stream().mapToInt(i->i).toArray();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…