I'm looking at the problem:
Write a program which reads a sequence of integers and displays them in ascending order.
I'm creating an ArrayList
(which I am new to) and I want to populate with integers input from the command line. With an array I could use a for
loop with:
for (int i =0; i < array.length; i++) {
array[i] = scanner.nextInt();
but with an ArrayList
of unbounded size I'm not sure how to process the input.
EDIT:
class SortNumbers {
public static void main(String[] args) {
List numbers = new ArrayList();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter some numbers.");
while (scanner.hasNextInt()) {
int i = scanner.nextInt();
numbers.add(i);
}
}
}
List numbers = new ArrayList();
will give rise to warnings based on raw types. Since you know (or assume) that the numbers will be integers you should define the list with type information, see my answer (or Piotr's). – Ashraf