/* * @topic T01705 List interface * @brief Main driver: List operations */ package pckg; /* * ArrayList, LinkedList, and Vector classes implement the List interface. * The List interface comes from the java.util package. * This program instantiates a LinkedList of Rectangle objects * and displays the content of the list. * * Then program removes an object using unique reference and displays the * LinkedList again. * * Build an array of Rectangle objects from the LinkedList * Dispaly the aray. * * Sort and display the sorted array of Rectangles. */ import java.util.LinkedList; import java.util.List; import java.util.Arrays; public class ListDemoL { public static void main(String args[]) { System.out.println( " Create a List of Rectangle objects"); List<Rectangle> rects = new LinkedList<Rectangle>(); /* OR List<Rectangle> rects = new ArrayList<Rectangle>(); * * OR List<Rectangle> rects = new Vector<Rectangle>; *************************************************************************/ rects.add(new Rectangle(30,30)); rects.add(new Rectangle(20,20)); rects.add(new Rectangle(10,10)); rects.add(new Rectangle(40,40)); Rectangle myrectangle = new Rectangle(9.0,9.0); // CREATED A UNIQUE RECTANGLE // OBJECT WITH A UNIQUE NAME rects.add(myrectangle); // ADDED A UNIQUE RECTANGLE WITH // A UNIQUE REFERENCE NAME System.out.println( " Display the dList of Rectangle objects"); for( Rectangle x : rects) { x.displayRectangle(); } System.out.println(); //******************************************************************************* rects.remove(myrectangle); // REMOVING A UNIQUE OBJECT BY // REFERENCE NAME System.out.println( " Display the List of Rectangle objects with one removed"); for( Rectangle x : rects) { System.out.println( x.getLength() + " by " + x.getWidth()); } System.out.println(); //********************************************************************************* /* Building an array of Rectangles from the List of Rectangles The syntax below instatiates an array of Rectangle objects named A with the objects from the rects List in a single statement!! */ System.out.println( " Building an array of Rectangle objects from the LinkedList"); int s = rects.size(); Rectangle [] A =new Rectangle[s]; rects.toArray(A); /**************************************************************************** The syntax below updates the array of Rectangle objects named A (which was instatiated above) with the objects from the rects LinkedList AND SORTS THEM them in a single statement!! Arrays.sort(rects.toArray(A)); ****************************************************************************/ // Displaying the array of Rectangles System.out.println("\n Display the array of Rectangle objects"); for( Rectangle x : A ) { x.displayRectangle(); } System.out.println(); //************************************************************************* // Sort and display the aray of Rectangles System.out.println( " Sort and Display the array of Rectangle" + " objects by length"); Arrays.sort(A); for( Rectangle x : A ) { x.displayRectangle(); } System.out.println(); //****************************************************************************** //Passing a unique object to method Rectangle yourrectangle = new Rectangle(12.0,12.0); // CREATED A UNIQUE RECTANGLE // OBJECT WITH A UNIQUE NAME rects.add(yourrectangle); // ADDED A UNIQUE RECTANGLE WITH // A UNIQUE REFERENCE NAME System.out.println( "The List of Rectangle objects" + " After a Unique Addition by Reference Name"); displayLinkedList(rects); passingRefrenceDemo(rects,yourrectangle); System.out.println( "The List of Rectangle objects" + " After a Unique Removal by Reference Name"); displayLinkedList(rects); } // end of main //******************************************************************************* public static void passingRefrenceDemo(List<Rectangle> rex, Rectangle yours) { rex.remove(yours); } // end of passingRerenceDemo( ) //****************************************************************************** public static void displayLinkedList(List<Rectangle> rex) { for( Rectangle x : rex) { System.out.println( x.getLength() + " by " + x.getWidth()); } System.out.println(); } // end of displayLinkedList( ) //****************************************************************************** } // end of class /* Create a LinkedList of Rectangle objects Display the LinkedList of Rectangle objects 30.0 by 30.0 20.0 by 20.0 10.0 by 10.0 40.0 by 40.0 9.0 by 9.0 Display the LinkedList of Rectangle objects with two removed 30.0 by 30.0 10.0 by 10.0 40.0 by 40.0 Building an array of Rectangle objects from the LinkedList Display the array of Rectangle objects 30.0 by 30.0 10.0 by 10.0 40.0 by 40.0 Sort and Display the array of Rectangle objects by length 10.0 by 10.0 30.0 by 30.0 40.0 by 40.0 The LinkedList of Rectangle objects After a Unique Addition by Reference Name 30.0 by 30.0 10.0 by 10.0 40.0 by 40.0 12.0 by 12.0 The LinkedList of Rectangle objects After a Unique Removal by Reference Name 30.0 by 30.0 10.0 by 10.0 40.0 by 40.0 Press any key to continue... */