/* * @topic T02005 ArrayList of ints * @brief Main driver: ArrayList operations */ package pckg; /* * The ints are "wrappped" and become objects of the Integer class. * * Program displays the contents of the ArrayList using * "autoboxing" for each Integer object. * * Program builds an array of ints by casting each Integer object * in the ArrayList. * * Program displays the array of ints. * * Finally program sorts and displays the sorted array of ints. * * Program demonstrates how a "unique object" can be added and removed * from the ArrayList. */ import java.util.*; public class ArrayListDemo { public static void main(String args []) { Scanner sc = new Scanner(System.in); ArrayList<Integer> nums = new ArrayList<Integer>(); // Loading the ArrayList System.out.println("Instantiating an ArrayList of Integrs"); int n; for(int x = 1; x < 6; x++) { n = Validator.getInt(sc, "Enter next integer "); nums.add(n); //creates Integer objects using autoboxing } //____________________________________________________________________ // Displaying the Contents of the ArrayList System.out.println("Displaying the contents of an ArrayList of Integers"); for(int x : nums) { System.out.println(x);// using autoboxing } System.out.println(); //____________________________________________________________________ // Adding a unique object Integer y = new Integer(6); // or Integer y = 50; nums.add(y); System.out.println("Displaying the contents after adding unique Object"); for(int x : nums) { System.out.println(x); } System.out.println(); //_________________________________________________________________________ //Removing a unique object nums.remove(y); System.out.println("Displaying the contents after removing unique Object"); for(int x : nums) { System.out.println(x); } System.out.println(); //______________________________________________________________________ // Building an array of ints from the ArrayList of Integer objects System.out.println("Building an array of ints from the ArrayList"); int s = nums.size(); int [] A =new int[s]; // size of array now = size of ArrayList int i = 0; for (int x : nums) // using autounboxing { A[i++]=x; } /**************************************************************************** * Build and sort an array of objects from an ArrayList * of objects, similar to sorting and building * an array of objects from a LinkedList. ****************************************************************************/ // Displaying the array of ints System.out.println("Displaying the array of ints"); for(int x : A) { System.out.println(x); } System.out.println(); //___________________________________________________________________ // Sorting and displaying the array of ints System.out.println("Sorting and Displaying the array of ints"); Arrays.sort(A); for(int x : A) { System.out.println(x); } } // end of main }// end of ArrayListDemo /* Instantiating an ArrayList of Integrs Enter next integer 14 Enter next integer 13 Enter next integer 12 Enter next integer 11 Enter next integer 10 Displaying the contents of an ArrayList of Integers 14 13 12 11 10 Displaying the contents after adding unique Object 14 13 12 11 10 23 Displaying the contents after removing unique Object 14 13 12 11 10 Building an array of ints from the ArrayList Displaying the array of ints 14 13 12 11 10 Sorting and Displaying the array of ints 10 11 12 13 14 Press any key to continue... */