1 / 20

Problem Solving # 9: ArrayList, Collections and More

Problem Solving # 9: ArrayList, Collections and More. ICS201-071. Outline. Review of Key Topics Problem 1: Using ArrayList Class … Problem 2: Iterators and ListIterators Problem 3: Applications of ArrayList …. Problem 4: Java Collections … Problem 5: Java Generics ….

kitty
Download Presentation

Problem Solving # 9: ArrayList, Collections and More

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Solving # 9: ArrayList, Collections and More ICS201-071

  2. Outline • Review of Key Topics • Problem 1: Using ArrayList Class … • Problem 2: Iterators and ListIterators • Problem 3: Applications of ArrayList …. • Problem 4: Java Collections … • 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<Seller> list = new ArrayList<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….

  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<Integer> list = new ArrayList<Integer>();    list.add(5);  // Automatic boxing    list.add(new Integer(6));   int value = list.get(1); System.out.println(value);   }}

  5. ArrayList Class: Tips (Cont’d) import java.util.*;public class ArrayListGenericDemo {public static void main(String[] args) {    ArrayList<String> data = new ArrayList<String>();    data.add("hello");    data.add("goodbye");    // data.add(new Date()); This won't compile!    Iterator<String> it = data.iterator();while (it.hasNext()) {      String s = it.next();      System.out.println(s);    }  }}

  6. Java Generics: Tips class GenCons {private double val;  <T extends Number> 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 <T, V extends T> 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 ) { HashSet<Integer> set = new HashSet<Integer>(); set.add( new Integer( 6 ) ); set.add( new Integer( 1 ) ); set.add( new Integer( 4 ) ); System.out.println( set ); System.out.println(); 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 ); } }

  9. 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 … • Search for a specific Student record using an iterator • 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 <String>collection = new HashSet <String>();    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: class Gen<T> {  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<Integer> iOb;    iOb = new Gen<Integer>(88);    iOb.showType();int v = iOb.getob();    System.out.println("value: " + v);    System.out.println();    Gen<String> strOb = new Gen<String>("Generics Test");    strOb.showType();    String str = strOb.getob();    System.out.println("value: " + str);  }}

More Related