Problem Solving # 9: ArrayList, Collections and More ICS
2 Outline Review of Key Topics Review of Key Topics Problem 1: Using ArrayList Class … Problem 1: Using ArrayList Class … Problem 2: Iterators and ListIterators Problem 2: Iterators and ListIterators Problem 3: Applications of ArrayList …. Problem 3: Applications of ArrayList …. Problem 4: Java Collections … Problem 4: Java Collections … Problem 5: Java Generics … Problem 5: Java Generics …
3 ArrayList Class: Tips import java.util.ArrayList; public class MyArrayList1 { public static void main(String[] args) { Seller sel = new Seller(); Buyer buy = new Buyer(); // declare an array list of type Seller ArrayList list = new ArrayList (); list.add(sel); // add object of type Seller which is permisible list.add(buy); // this statement will give an error } } // Classes Seller and Buyer are supposed defined earlier….
4 ArrayList Class: Tips (Cont’d) import java.util.ArrayList; public class MyArrayList2 { public static void main(String[] args) { // declare an array list of Integer ArrayList list = new ArrayList (); list.add(5); // Automatic boxing list.add(new Integer(6)); list.add(new Integer(6)); int value = list.get(1); int value = list.get(1); System.out.println(value); } } System.out.println(value); } }
5 ArrayList Class: Tips (Cont’d) import java.util.*; public class ArrayListGenericDemo { public static void main(String[] args) { ArrayList data = new ArrayList (); data.add("hello"); data.add("goodbye"); // data.add(new Date()); This won't compile! Iterator it = data.iterator(); while (it.hasNext()) { String s = it.next(); System.out.println(s); } } }
6 Java Generics: Tips class GenCons { private double val; GenCons(T arg) { val = arg.doubleValue(); } void showval() { System.out.println("val: " + val); } } public class GenConsDemo { public static void main(String args[]) { GenCons test = new GenCons(100); GenCons test2 = new GenCons(123.5F); test.showval(); test2.showval(); } }
7 Java Generics: Tips (Con’d) public class GenMethDemo { // Determine if an object is in an array. static boolean isIn(T x, V[] y) { for(int i=0; i boolean isIn(T x, V[] y) { for(int i=0; i < y.length; i++) if(x.equals(y[i])) return true; return false; } public static void main(String args[]) { // Use isIn() on Integers. Integer nums[] = { 1, 2, 3, 4, 5 }; if(isIn(2, nums)) System.out.println("2 is in nums"); if(!isIn(7, nums)) System.out.println("7 is not in nums"); System.out.println(); // Use isIn() on Strings. String strs[] = { "one", "two", "three", "four", "five" }; if(isIn("two", strs)) System.out.println("two is in strs"); if(!isIn("seven", strs)) System.out.println("seven is not in strs"); // Opps! Won't compile! Types must be compatible. // if(isIn("two", nums)) // System.out.println("two is in strs"); } }
8 HashSet: Tips import java.util.*; public class HashSet1 { public static void main( String[] args ) { public static void main( String[] args ) { HashSet set = new HashSet (); HashSet set = new HashSet (); set.add( new Integer( 6 ) ); set.add( new Integer( 6 ) ); set.add( new Integer( 1 ) ); set.add( new Integer( 1 ) ); set.add( new Integer( 4 ) ); set.add( new Integer( 4 ) ); System.out.println( set ); System.out.println( set ); System.out.println(); System.out.println(); System.out.println( "Show that duplicates cannot be added." ); System.out.println( "Show that duplicates cannot be added." ); set.add( new Integer( 8 ) ); set.add( new Integer( 8 ) ); System.out.println( "New contents are " + set ); System.out.println( "New contents are " + set ); boolean value = set.add( new Integer( 4 ) ); boolean value = set.add( new Integer( 4 ) ); if(value) if(value) System.out.println(" A duplicate value has been added!"); System.out.println(" A duplicate value has been added!"); else else System.out.println(" A duplicate value can NOT be added!"); System.out.println(" A duplicate value can NOT be added!"); System.out.println( "New contents are " + set ); System.out.println( "New contents are " + set ); } }
Form Groups of 3 Students and Work on the Following Problem
10 Problem 1: Using ArrayList Class to store Student data. Student data: id, name, gpa and major
11 Problem 1 … a) Define the Student class b) Write the test class c) Search for a specific Student record
12 Solution
13 Problem 2 … a) Search for a specific Student record using an iterator b) Remove all the duplicate copies of a specific Student record using an iterator
14 Solution
15 Problem 3 Swapping a list of ArrayList elements: a) Using the for-each loop c) Using iterators (….)
16 Solution
17 Problem 4 Consider the following code…
18 Problem 4 import java.util.*; public class CollectionTest { public static void main(String [] args) { System.out.println( "Collection Example!\n" ); int size; // Create a collection HashSet collection = new HashSet (); String str1 = "Yellow", str2 = "White", str3 = "Green", str4 = "Blue"; Iterator iterator; //Adding data in the collection collection.add(str1); collection.add(str2); collection.add(str3); collection.add(str4); System.out.print("Collection data: "); //Create a iterator iterator = collection.iterator(); while (iterator.hasNext()){ System.out.print(iterator.next() + " "); } System.out.println(); // Get size of a collection size = collection.size(); if (collection.isEmpty()){ System.out.println("Collection is empty"); } else{ System.out.println( "Collection size: " + size); } System.out.println(); import java.util.*; public class CollectionTest { public static void main(String [] args) { System.out.println( "Collection Example!\n" ); int size; // Create a collection HashSet collection = new HashSet (); String str1 = "Yellow", str2 = "White", str3 = "Green", str4 = "Blue"; Iterator iterator; //Adding data in the collection collection.add(str1); collection.add(str2); collection.add(str3); collection.add(str4); System.out.print("Collection data: "); //Create a iterator iterator = collection.iterator(); while (iterator.hasNext()){ System.out.print(iterator.next() + " "); } System.out.println(); // Get size of a collection size = collection.size(); if (collection.isEmpty()){ System.out.println("Collection is empty"); } else{ System.out.println( "Collection size: " + size); } System.out.println(); // Remove specific data collection.remove(str2); System.out.println("After removing [" + str2 + "]\n"); System.out.print("Now collection data: "); iterator = collection.iterator(); while (iterator.hasNext()){ System.out.print(iterator.next() + " "); } System.out.println(); size = collection.size(); System.out.println("Collection size: " + size + "\n"); //Collection empty collection.clear(); size = collection.size(); if (collection.isEmpty()){ System.out.println("Collection is empty"); } else{ System.out.println( "Collection size: " + size); } } }
19 Problem 5 Define a generic class as follows: Define a generic class as follows: class Gen { T ob; // declare an object of type T Gen(T o) { ob = o; } T getob() { return ob; } void showType() { System.out.println("Type of T is " + ob.getClass().getName()); } }
20 Problem 5 (Cont’d) public class GenDemo { public static void main(String args[]) { Gen iOb; iOb = new Gen (88); iOb.showType(); int v = iOb.getob(); System.out.println("value: " + v); System.out.println(); Gen strOb = new Gen ("Generics Test"); strOb.showType(); String str = strOb.getob(); System.out.println("value: " + str); } }