ArrayLists: Collections Framework
Collections Framework unlike fixed-length arrays provides for the ability to produce variable length lists. There are a set of 7 different types of containers in the Collections Framework (List, Set, Map, Collections, Iterator, Comparator, Comparable). These containers cannot store primitives (use wrapper classes Integer, Double, etc. to create Objects).
List & Collection synonyms for a container.
Picture on page 432
Collections classis a helper class that supports the container
class. Allows you to search, sort, fill shuffle,
replace, reverse
Iterator class: Determines if more elements exist and returns the next element.
Comparator class: Used for sorting elements in a list
(default order)
Comparable class: Orders element to a supplied ordering
Sequence (used for sorting)
ArrayList
An ArrayLists expand and contract as needed.
ArarryList Structure picture p433
p436 Chart of ArrayList Class Methods
Other great ArrayList notes from Lawrenceville Press:
The Object class you are storing in the ArrayList must have an overridden equals() method.
How the ArrayList is sized used Capacity
Capacity is initially 10 and increase by 50% each time using
New Capacity = (3* Old Capacity)/2 + 1
Wrapper Classes Example
ArrayList<Integer> numlist = new ArrayList<Integer>();
numlist.add(0,new Integer(28)); ///called boxing
int n=numlist.get(0).intValue(); ///called unboxing
System.out.println();
Autoboxing implicitly converts primitive type data to reference
type.
ArrayList<Integer> numlist = new ArrayList<Integer>();
numlist.add(28);
int n=numlist.get(0);
System.out.println();
Collections Helper Classes
Summary of Commonly Used Collections Class Methods:
- int binarySearch(List list,Object obj)
- void copy(List list1,List list2)
- void fill(List list, Object obj)
- Object max(Collection list) //returns max element using natural ordering
- Object min(Collection list) //returns min element using natural ordering
- void reverse(List list)
- void shuffle(List list)
- void sort(List list)
Refer to page 438 for Table 8.3
Example on page 439
Iterator Class
Keeps track of an object’s position within a container.
Iterator Methods:
- boolean hasNext() //returns true if the container has more elements
- element next() //returns the next element and advances the iterator
- void remove() //removes the element from the current iterator position
Iterator Example Code
int sum=0,intvalue;
ArrayList<Integer> x = new ArrayList<Integer>();
x.add(1);
x.add(2);
x.add(3);
x.add(4);
Iterator iter = x.iterator();
while (iter.hasNext())
{
intvalue= (Integer) iter.next();
sum=sum+intvalue;
}
Assignment:Create an Inventory Control System. You need to create a class that has the data members (product number, product description, product quantity and product price). Provide all the necessary constructors, accessors, modifiers and any other methods that you feel you need. Please allow the user to add, remove, update and list inventory records using an ArrayList. Note product number should be unique.