Written by Chris Piech
Handouts:
ArrayLists
Examples:
Rockets
Day7: SortedNumbers.java
Ask the user to enter 10 numbers, then print thoses numbers in sorted order.
Store the numbers that the user entered in an ArrayList. You can sort the content of an ArrayList using:
Collections.sort(arrayList);Calling sort re-orders the elements in the array passed in. For example, calling sort on the following array has the given result:
To create an array list of numbers use:
ArrayList<Integer> myList = new ArrayList<Integer>();
Recall that you can read an intenger from the user using:
int number = readInt(prompt);
To add an element to a list:
myList.add(newValue);
To get an element from a list:
myList.get(index);
To loop over all elements in a list:
for(int i = 0; i < myList.size(); i++) { myList.get(i); }