














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















All classes provides almost same methods
get(), size(), isEmpty()…
Easy learning curve for using Collections
Implemented as reference to Object
Similar to using a void * in C
Require a cast back (down casting) to the actual type
Example String element = (String)arraylist.get(i)
Remember, Java checks all casts at run-time
Replaces the “Vector”
Can grow over time
Methods
add(Object) Can add all kinds of objects implicit upcasting int size() Object get(int index) Index is from 0 to size() - Must cast to appropriate type when used. remove(index) Removes object reference at the specified index iterator() We’ll see an example!
Example TestArrayList. java
//checking whether arraylist contains student objects or not
boolean b = al.isEmpty();
if (b == true){ System.out.println("arraylist is empty"); } else { int size = al.size(); System.out.println("arraylist size: "+ size); }
Example TestArrayList. java
//using loop to iterate for (int i=0; i< al.size(); i++) { Student s = (Student) al.get(i); s.print(); }
} //end of main }
Stores key & value in pair form
Cannot contain duplicate keys
Implements the Map interface
Keys and Values are stored as Objects
put(Key, Value) implicit upcasting
Object get(Key) Must cast to appropriate type when used.
int size()
Example TestHashMap. java
boolean b = h.isEmpty();
if (b == true){ System.out.println("hashmap is empty"); } else { int size = h.size(); System.out.println("hashmap size:"+ size); }
Student s = (Student) h.get("two"); s.print();
} //end of main }
Compile & Execute
How to Perform IO
Selection and Control Structures
OOP
Collections
So, lets do a small problem
Problem
Address Book