
b8518c85859900916a395e53138743a6.ppt
- Количество слайдов: 20
Problem Solving # 9: Array. List, Collections and More ICS 201 -071
Outline Review of Key Topics n Problem 1: Using Array. List Class … n Problem 2: Iterators and List. Iterators n Problem 3: Applications of Array. List …. n Problem 4: Java Collections … n Problem 5: Java Generics … n 2
Array. List Class: Tips import java. util. Array. List; public class My. Array. List 1 { public static void main(String[] args) { Seller sel = new Seller(); Buyer buy = new Buyer(); // declare an array list of type Seller Array. List<Seller> list = new Array. List<Seller>(); 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…. 3
Array. List Class: Tips (Cont’d) import java. util. Array. List; public class My. Array. List 2 { public static void main(String[] args) { // declare an array list of Integer Array. List<Integer> list = new Array. List<Integer>(); list. add(5); // Automatic boxing list. add(new Integer(6)); int value = list. get(1); System. out. println(value); } } 4
Array. List Class: Tips (Cont’d) import java. util. *; public class Array. List. Generic. Demo { public static void main(String[] args) { Array. List<String> data = new Array. List<String>(); data. add("hello"); data. add("goodbye"); // data. add(new Date()); This won't compile! Iterator<String> it = data. iterator(); while (it. has. Next()) { String s = it. next(); System. out. println(s); } } } 5
Java Generics: Tips class Gen. Cons { private double val; <T extends Number> Gen. Cons(T arg) { val = arg. double. Value(); } void showval() { System. out. println("val: " + val); } } public class Gen. Cons. Demo { public static void main(String args[]) { Gen. Cons test = new Gen. Cons(100); Gen. Cons test 2 = new Gen. Cons(123. 5 F); test. showval(); test 2. showval(); } } 6
Java Generics: Tips (Con’d) public class Gen. Meth. Demo { // Determine if an object is in an array. static <T, V extends T> boolean is. In(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 is. In() on Integers. Integer nums[] = { 1, 2, 3, 4, 5 }; if(is. In(2, nums)) System. out. println("2 is in nums"); if(!is. In(7, nums)) System. out. println("7 is not in nums"); System. out. println (); // Use is. In() on Strings. String strs[] = { "one", "two", "three", "four", "five" }; if(is. In("two", strs)) System. out. println("two is in strs"); if(!is. In("seven ", strs)) System. out. println("seven is not in strs"); // Opps! Won't compile! Types must be compatible. // if(is. In("two", nums)) // System. out. println("two is in strs"); } } 7
Hash. Set: Tips import java. util. *; public class Hash. Set 1 { public static void main( String[] args ) { Hash. Set<Integer> set = new Hash. Set<Integer>(); set. add( new Integer( 6 ) ); set. add( new Integer( 1 ) ); set. add( new Integer( 4 ) ); System. out. println( set ); System. out. println( "Show that duplicates cannot be added. " ); set. add( new Integer( 8 ) ); System. out. println( "New contents are " + set ); boolean value = set. add( new Integer( 4 ) ); if(value) System. out. println(" A duplicate value has been added!"); else System. out. println(" A duplicate value can NOT be added!"); System. out. println( "New contents are " + set ); } } 8
Form Groups of 3 Students and Work on the Following Problem
Problem 1: Using Array. List Class to store Student data: id, name, gpa and major 10
Problem 1 … a) Define the Student class b) Write the test class c) Search for a specific Student record 11
Solution 12
Problem 2 … a) b) Search for a specific Student record using an iterator Remove all the duplicate copies of a specific Student record using an iterator 13
Solution 14
Problem 3 Swapping a list of Array. List elements: a) Using the for-each loop c) Using iterators (…. ) 15
Solution 16
Problem 4 Consider the following code… 17
Problem 4 import java. util. *; public class Collection. Test { public static void main(String [] args) { System. out. println ( "Collection Example!n" ); int size; // Create a collection Hash. Set <String>collection = new Hash. Set <String>(); String str 1 = "Yellow", str 2 = "White", str 3 = "Green", str 4 = "Blue"; Iterator iterator; //Adding data in the collection. add(str 1); collection. add(str 2); // Remove specific data collection. add(str 3); collection. remove(str 2); collection. add(str 4); System. out. println("After removing [" + str 2 + "]n"); System. out. print("Collection data: "); System. out. print("Now collection data: "); //Create a iterator = collection. iterator(); while (iterator. has. Next()){ System. out. print(iterator. next () + " "); } } System. out. println (); size = collection. size(); // Get size of a collection System. out. println("Collection size: " + size + "n"); size = collection. size(); //Collection empty if (collection. is. Empty()){ collection. clear(); System. out. println("Collection is empty"); size = collection. size(); } if (collection. is. Empty()){ else{ System. out. println("Collection is empty"); System. out. println ( "Collection size: " + size); } } else{ System. out. println (); System. out. println ( "Collection size: " + size); } } } 18
Problem 5 n Define a generic class as follows: class Gen<T> { T ob; // declare an object of type T Gen(T o) { ob = o; } T getob() { return ob; } void show. Type() { System. out. println("Type of T is " + ob. get. Class(). get. Name()); } } 19
Problem 5 (Cont’d) public class Gen. Demo { public static void main(String args[]) { Gen<Integer> i. Ob; i. Ob = new Gen<Integer>(88); i. Ob. show. Type(); int v = i. Ob. getob(); System. out. println("value: " + v); System. out. println(); Gen<String> str. Ob = new Gen<String>("Generics Test"); str. Ob. show. Type(); String str = str. Ob. getob(); System. out. println("value: " + str); } } 20
b8518c85859900916a395e53138743a6.ppt