1 / 83

COP 3503 FALL 2012 Shayan Javed Lecture 8

COP 3503 FALL 2012 Shayan Javed Lecture 8. Programming Fundamentals using Java. ArrayList and Java Generics. Collection. A container object that groups multiple objects. Collection. A container object that groups multiple objects Example: Arrays. Collection.

afya
Download Presentation

COP 3503 FALL 2012 Shayan Javed Lecture 8

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. COP 3503 FALL 2012ShayanJavedLecture 8 Programming Fundamentals using Java

  2. ArrayList and Java Generics

  3. Collection • A container object that groups multiple objects

  4. Collection • A container object that groups multiple objects • Example: Arrays

  5. Collection • A container object that groups multiple objects • Example: Arrays • Java provides multiple classes/interfaces in the Java Collection Framework

  6. Collection • A container object that groups multiple objects • Example: Arrays • Java provides multiple classes/interfaces in the Java Collection Framework • Going to look at ArrayList

  7. Arrays • Hold several elements (primitives/objects) of the same type

  8. Arrays • Hold several elements (primitives/objects) of the same type • Size = static. • Once set, cannot be changed

  9. Arrays • Advantages: • Easy to access elements directly

  10. Arrays • Advantages: • Easy to access elements directly • Easy to traverse

  11. Arrays • Advantages: • Easy to access elements directly • Easy to traverse • Disadvantages: • Have to decide on size before creating

  12. Arrays • Advantages: • Easy to access elements directly • Easy to traverse • Disadvantages: • Have to decide on size before creating • Inserting/removing elements is troublesome • Shifting elements

  13. ArrayList (java.util.ArrayList) • Holds several objects in order (like Arrays)

  14. ArrayList (java.util.ArrayList) • Holds several objects in order (like Arrays) • Unlike Arrays: • Size does not need to be specified at creation time

  15. ArrayList (java.util.ArrayList) • Holds several objects in order (like Arrays) • Unlike Arrays: • Size does not need to be specified at creation time • Grows in size as items added

  16. ArrayList (java.util.ArrayList) • Holds several objects in order (like Arrays) • Unlike Arrays: • Size does not need to be specified at creation time • Grows in size as items added • Provides useful methods for manipulating the list

  17. ArrayList (java.util.ArrayList) • Constructors: • newArrayList( ) - no need to pass in size (10)

  18. ArrayList (java.util.ArrayList) • Constructors: • newArrayList( ) - no need to pass in size (10) • newArrayList(int) - initial capacity

  19. ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list

  20. ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list • void add(int index, Object o): Inserts object at specified index

  21. ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list • void add(int index, Object o): Inserts object at specified index • void clear(): Removes all elements

  22. ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list • void add(int index, Object o): Inserts object at specified index • void clear(): Removes all elements • boolean contains(Object o): Returns true if object is in the list

  23. ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list • void add(int index, Object o): Inserts object at specified index • void clear(): Removes all elements • boolean contains(Object o): Returns true if object is in the list • Object get(int index): Returns object at the index

  24. ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list • void add(int index, Object o): Inserts object at specified index • void clear(): Removes all elements • boolean contains(Object o): Returns true if object is in the list • Object get(int index): Returns object at the index • boolean remove(Object o): Removes the object from the list

  25. ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list • void add(int index, Object o): Inserts object at specified index • void clear(): Removes all elements • boolean contains(Object o): Returns true if object is in the list • Object get(int index): Returns object at the index • boolean remove(Object o): Removes the object from the list • Object remove(int index): Removes the object at the index

  26. ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list • void add(int index, Object o): Inserts object at specified index • void clear(): Removes all elements • boolean contains(Object o): Returns true if object is in the list • Object get(int index): Returns object at the index • boolean remove(Object o): Removes the object from the list • Object remove(int index): Removes the object at the index • intsize(): Returns the no. of elements in the list

  27. ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list • void add(int index, Object o): Inserts object at specified index • void clear(): Removes all elements • boolean contains(Object o): Returns true if object is in the list • Object get(int index): Returns object at the index • boolean remove(Object o): Removes the object from the list • Object remove(int index): Removes the object at the index • intsize(): Returns the no. of elements in the list • intindexOf(Object o): Returns index of first matching element

  28. ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list • void add(int index, Object o): Inserts object at specified index • void clear(): Removes all elements • boolean contains(Object o): Returns true if object is in the list • Object get(int index): Returns object at the index • boolean remove(Object o): Removes the object from the list • Object remove(int index): Removes the object at the index • intsize(): Returns the no. of elements in the list • intindexOf(Object o): Returns index of first matching element • Object set(int index, Object o): Sets the object at the index

  29. ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list • void add(int index, Object o): Inserts object at specified index • void clear(): Removes all elements • boolean contains(Object o): Returns true if object is in the list • Object get(int index): Returns object at the index • boolean remove(Object o): Removes the object from the list • Object remove(int index): Removes the object at the index • intsize(): Returns the no. of elements in the list • intindexOf(Object o): Returns index of first matching element • Object set(int index, Object o): Sets the object at the index • Object clone(): Returns a shallow copy

  30. ArrayList Example import java.util.ArrayList;

  31. ArrayList Example import java.util.ArrayList; ArrayList numbers = newArrayList(); // no size specified initially

  32. ArrayList Example import java.util.ArrayList; ArrayList numbers = newArrayList(); // no size specified initially numbers.add(newInteger(10));

  33. ArrayList Example import java.util.ArrayList; ArrayList numbers = newArrayList(); // no size specified initially numbers.add(newInteger(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */

  34. ArrayList Example import java.util.ArrayList; ArrayList numbers = newArrayList(); // no size specified initially numbers.add(newInteger(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */ int sum = 0; // sum up all the numbers in the list int number = 0;

  35. ArrayList Example import java.util.ArrayList; ArrayList numbers = newArrayList(); // no size specified initially numbers.add(newInteger(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */ int sum = 0; // sum up all the numbers in the list int number = 0; for(inti = 0; i < numbers.size(); i++) { }

  36. ArrayList Example import java.util.ArrayList; ArrayList numbers = newArrayList(); // no size specified initially numbers.add(newInteger(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */ int sum = 0; // sum up all the numbers in the list int number = 0; for(inti = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // cast required – why? }

  37. ArrayList Example import java.util.ArrayList; ArrayList numbers = newArrayList(); // no size specified initially numbers.add(newInteger(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */ int sum = 0; // sum up all the numbers in the list int number = 0; for(inti = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // cast required – why? sum += number; // auto un-boxing: Integer converted to int }

  38. ArrayList Example import java.util.ArrayList; ArrayList numbers = newArrayList(); // no size specified initially numbers.add(newInteger(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */ numbers.add(“A String”); // will compile! int sum = 0; // sum up all the numbers in the list int number = 0; for(inti = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // cast required – why? sum += number; // auto un-boxing: Integer converted to int }

  39. ArrayList Example import java.util.ArrayList; ArrayList numbers = newArrayList(); // no size specified initially numbers.add(newInteger(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */ numbers.add(“A String”); // will compile! int sum = 0; // sum up all the numbers in the list int number = 0; for(inti = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // run-time error when i = 2 sum += number; // auto un-boxing: Integer converted to int }

  40. ArrayList How do we make sure we are dealing with objects of only one type?

  41. ArrayList How do we make sure we are dealing with objects of only one type? Solution: Java Generics

  42. Java Generics • Generics is the capability to parameterize types

  43. Java Generics • Generics is the capability to parameterize types • Can define class/interface/method with generic types – compiler replaces with concrete types.

  44. Java Generics • Generics is the capability to parameterize types • Can define class/interface/method with generic types – compiler replaces with concrete types. • Introduced in JDK 1.5

  45. java.util.ArrayList<E> • Methods: • boolean add(E o): Appends object at the end of the list • void add(int index, E o): Inserts object at specified index • void clear(): Removes all elements • boolean contains(E o): Returns true if object is in the list • E get(intindex): Returns object at the index • boolean remove(E o): Removes the object from the list • E remove(intindex): Removes the object at the index • intsize(): Returns the no. of elements in the list • intindexOf(Eo): Returns index of first matching element • E set(intindex, E o): Sets the object at the index • E clone(): Returns a shallow copy

  46. Java Generics • Type of objects it can hold is specified at declaration

  47. Java Generics • Type of objects it can hold is specified at declaration // will only hold Integer objects ArrayList<Integer> numbers = newArrayList<Integer>();

  48. ArrayList Example import java.util.ArrayList; ArrayList<Integer> numbers = newArrayList<Integer>(); numbers.add(newInteger(10)); numbers.add(3); /* auto-boxing: */ numbers.add(“A String”); // WON’T COMPILE! int sum = 0; // sum up all the numbers in the list int number = 0; for(inti = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // cast NOT required – why? sum += number; // auto un-boxing: Integer converted to int }

  49. ArrayList Example import java.util.ArrayList; ArrayList<Integer> numbers = newArrayList<Integer>(); numbers.add(newInteger(10)); numbers.add(3); /* auto-boxing: */ numbers.add(“A String”); // WON’T COMPILE! int sum = 0; // sum up all the numbers in the list int number = 0; for(inti = 0; i < numbers.size(); i++) { number = numbers.get(i); // cast NOT required – why? sum += number; // auto un-boxing: Integer converted to int }

  50. Java Generics • More strictness

More Related