//LinkedListTest.java //exact same as ArrayListTest.java //did global replace of ArrayList with LinkedList //LinkedList is almost the same as ArrayList. //The difference is that it is implemented by a linked list instead of an //array like ArrayList and so compared to ArrayList it is: // faster insert/delete (if location to insert or delete is already known), // insert/delete at either end is very fast and so the LinkedList is good // for a stack or queue. // slightly slower iteration.??? // much slower random access (using an index). //Methods are the same as ArrayList. It has some additional ones for inserting //and deleting at the beginning and end of the list but these can be done by //get, add, and remove methods. // http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html import java.util.*; //all the Collections Framework classes are here import javax.swing.*; class LinkedListTest { 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 LinkedList myLinkedList = new LinkedList(); 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); myLinkedList.add(new Integer(randInt)); //must wrap int into Integer } //iterate over all the objects in the arraylist collection. Iterator it=myLinkedList.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. myLinkedList.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 ); myLinkedList.add(userstring); } //must re-initialize the iterator it=myLinkedList.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 " + myLinkedList.size() + " objects in the arraylist"); if (myLinkedList.isEmpty()) System.out.println(" the arraylist is empty"); else System.out.println(" the arraylist is not empty (duhh)"); //some of the algorithms of the Collections class userAnswer = JOptionPane.showConfirmDialog(null, "Do you want to sort the list?", "Sort 'em?", JOptionPane.YES_NO_OPTION); if (userAnswer == JOptionPane.YES_OPTION) { Collections.sort(myLinkedList); //static method of Collections class it=myLinkedList.iterator(); System.out.println(); System.out.println("Here are your sorted strings: "); while (it.hasNext()) System.out.print(it.next() + " "); System.out.println(); } userAnswer = JOptionPane.showConfirmDialog(null, "Do you want to reverse the list?", "Reverse 'em?", JOptionPane.YES_NO_OPTION); if (userAnswer == JOptionPane.YES_OPTION) { Collections.reverse(myLinkedList); //static method of Collections class it=myLinkedList.iterator(); System.out.println(); System.out.println("Here are your reversed strings: "); while (it.hasNext()) System.out.print(it.next() + " "); System.out.println(); } userAnswer = JOptionPane.showConfirmDialog(null, "Do you want to shuffle the list?", "Shuffle 'em?", JOptionPane.YES_NO_OPTION); if (userAnswer == JOptionPane.YES_OPTION) { Collections.shuffle(myLinkedList); //static method of Collections class it=myLinkedList.iterator(); System.out.println(); System.out.println("Here are your shuffled strings: "); while (it.hasNext()) System.out.print(it.next() + " "); System.out.println(); } System.out.println("Min value in arraylist: " + Collections.min(myLinkedList)); System.out.println("Max value in arraylist: " + Collections.max(myLinkedList)); } }