//ArrayListTest.java //generate random ints, add to an arraylist, display them. // then input strings from user, add to an arraylist, display them. //Iterate over the arraylist. Use Collections algorithms. //ArrayList is one of the built-in classes of the Java Collections Framework. //ArrayList is a Collection of (differently-typed) objects featuring: // size expands and shrinks automatically as needed, i.e. dynamic array. // objects are ordered in the order they were added (they are not sorted), // and in their inserted order. // can add, search for, and remove objects. // fast random access (using an index). // "slow" insert/delete. // fast iteration. // Methods: // add(object) to end of list. duplicates allowed // add(index,object) insert before index, others shifted rightward // set(index,object) overwrite object at index with this object // remove(object) if it is in the arraylist // remove(index) delete object at this index, others shifted left // clear() remove all objects // contains(object) returns true if object is in arraylist // size() returns int that is number of objects in the arraylist // isEmpty() returns true if arraylist has 0 objects in it // get(index) returns Object at this index // indexOf(object) returns index if object found // iterator() returns an Iterator (see example below) // more... // http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html //Can sort, copy, reverse, shuffle etc. the ArrayList using static methods // of the Collections class. //Basically, does everything and much more that an array can do without the //tedious and error-prone array manipulation code. //Minor drawback is that array can be more efficient (faster) because it is //simpler. But few applications need this efficiency. //How to use an ArrayList: //1. declare and instantiate an ArrayList object: // ArrayList myArrayList = new ArrayList(); //2. use the methods on it: // myArrayList.add("a string object"); // myArrayList.add(anyObject); // if (myArrayList.contains(someObject))... import java.util.*; //**all the Collections Framework classes are here import javax.swing.*; public class ArrayListTest { public static void main(String[] args) { String input, userstring; int size; int range; int randInt; int userAnswer; //**indexed list of (any kind of) objects (but not primitives). //**size expands as needed ArrayList myArrayList = new ArrayList(); input = JOptionPane.showInputDialog( "Enter number of random ints to generate" ); size = Integer.parseInt( input ); input = JOptionPane.showInputDialog( "Enter upper range of the random ints" ); range = Integer.parseInt( input ); for (int i=1; i<=size; i++) { //i is not an index, just a loop counter randInt = (int)(Math.random()*range); //random int between 0 and range-1 //uncomment if this code confuses you: //System.out.println("generated int: " + randInt); myArrayList.add(new Integer(randInt)); //**must wrap int into Integer } //**iterate over all the objects in the arraylist collection. Iterator it=myArrayList.iterator(); //**an iterator is for accessing all the objects of collection, one by one. //**hasNext() is true until reach end of the collection. //**next() returns the next object in the iteration. //**notice no for loop looping over indexes! System.out.println("Here are your random integers: "); while (it.hasNext()) //display them on one line, separated by a blank System.out.print(it.next() + " "); System.out.println(); //is this cool or what? //now get strings from the user into an arraylist. //we should use another arraylist but I want to show that anythings //can be in an arraylist. myArrayList.clear(); //**remove all the previous contents input = JOptionPane.showInputDialog( "Enter number of strings you want to input" ); size = Integer.parseInt( input ); for (int i=1; i<=size; i++) { userstring = JOptionPane.showInputDialog( "Enter string #" + i ); myArrayList.add(userstring); //**add String object to arraylist } //**must re-initialize the iterator to use it again it=myArrayList.iterator(); System.out.println(); System.out.println("Here are your strings: "); while (it.hasNext()) System.out.print(it.next() + " "); System.out.println(); System.out.println(" there are " + myArrayList.size() + //** #objects in it now " objects in the arraylist"); if (myArrayList.isEmpty()) //** true if size is 0 System.out.println(" the arraylist is empty"); else System.out.println(" the arraylist is not empty"); //**some of the algorithms of the Collections class //** sort userAnswer = JOptionPane.showConfirmDialog(null, "Do you want to sort the list?", "Sort 'em?", JOptionPane.YES_NO_OPTION); if (userAnswer == JOptionPane.YES_OPTION) { Collections.sort(myArrayList); //**static method of Collections class it=myArrayList.iterator(); System.out.println(); System.out.println("Here are your sorted strings: "); while (it.hasNext()) System.out.print(it.next() + " "); System.out.println(); } //** reverse userAnswer = JOptionPane.showConfirmDialog(null, "Do you want to reverse the list?", "Reverse 'em?", JOptionPane.YES_NO_OPTION); if (userAnswer == JOptionPane.YES_OPTION) { Collections.reverse(myArrayList); //**static method of Collections class it=myArrayList.iterator(); System.out.println(); System.out.println("Here are your reversed strings: "); while (it.hasNext()) System.out.print(it.next() + " "); System.out.println(); } //** shuffle (randomly mixup, i.e. permute) userAnswer = JOptionPane.showConfirmDialog(null, "Do you want to shuffle the list?", "Shuffle 'em?", JOptionPane.YES_NO_OPTION); if (userAnswer == JOptionPane.YES_OPTION) { Collections.shuffle(myArrayList); //**static method of Collections class it=myArrayList.iterator(); System.out.println(); System.out.println("Here are your shuffled strings: "); while (it.hasNext()) System.out.print(it.next() + " "); System.out.println(); } //** smallest and largest values System.out.println("Min value in arraylist: " + Collections.min(myArrayList)); System.out.println("Max value in arraylist: " + Collections.max(myArrayList)); } }