830 likes | 959 Views
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.
E N D
COP 3503 FALL 2012ShayanJavedLecture 8 Programming Fundamentals using Java
Collection • A container object that groups multiple objects
Collection • A container object that groups multiple objects • Example: Arrays
Collection • A container object that groups multiple objects • Example: Arrays • Java provides multiple classes/interfaces in the Java Collection Framework
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
Arrays • Hold several elements (primitives/objects) of the same type
Arrays • Hold several elements (primitives/objects) of the same type • Size = static. • Once set, cannot be changed
Arrays • Advantages: • Easy to access elements directly
Arrays • Advantages: • Easy to access elements directly • Easy to traverse
Arrays • Advantages: • Easy to access elements directly • Easy to traverse • Disadvantages: • Have to decide on size before creating
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
ArrayList (java.util.ArrayList) • Holds several objects in order (like Arrays)
ArrayList (java.util.ArrayList) • Holds several objects in order (like Arrays) • Unlike Arrays: • Size does not need to be specified at creation time
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
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
ArrayList (java.util.ArrayList) • Constructors: • newArrayList( ) - no need to pass in size (10)
ArrayList (java.util.ArrayList) • Constructors: • newArrayList( ) - no need to pass in size (10) • newArrayList(int) - initial capacity
ArrayList (java.util.ArrayList) • Methods: • boolean add(Object o): Appends object at the end of the list
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
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
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
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
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
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
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
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
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
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
ArrayList Example import java.util.ArrayList;
ArrayList Example import java.util.ArrayList; ArrayList numbers = newArrayList(); // no size specified initially
ArrayList Example import java.util.ArrayList; ArrayList numbers = newArrayList(); // no size specified initially numbers.add(newInteger(10));
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)); */
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;
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++) { }
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? }
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 }
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 }
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 }
ArrayList How do we make sure we are dealing with objects of only one type?
ArrayList How do we make sure we are dealing with objects of only one type? Solution: Java Generics
Java Generics • Generics is the capability to parameterize types
Java Generics • Generics is the capability to parameterize types • Can define class/interface/method with generic types – compiler replaces with concrete types.
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
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
Java Generics • Type of objects it can hold is specified at declaration
Java Generics • Type of objects it can hold is specified at declaration // will only hold Integer objects ArrayList<Integer> numbers = newArrayList<Integer>();
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 }
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 }
Java Generics • More strictness