- Case Study: Vocabulary Comparison
-
The Comparable Interface
- Collections.sort(words)
-
Natural Ordering and compareTo
- Comparison Function
- Natural Ordering
-
ArrayLists do not have natural ordering
- Comparable interface
- public interface Comparable<T> {public int compareTo(T other);}
- Collections.sort(); is in the Comparable interface
-
ArrayLists
-
basics
- no need to worry about add/remove strategies
-
Generic Class
- ArrayList<E>
-
Basic ArrayList Operations
- import java.util.*
-
methods
-
add
-
add(index, <value>)
- expensive in time
-
remove
-
remove(index);
- expensive in time
-
replace
- <name>.set(index, newValue)
-
remove all
- clear
-
no. of elements
- <name>.size()
-
obtain item
- <name>.get(index)
- dynamic
-
ArrayList Searching Methods
- contains
-
indexOf
- first occurrence
-
lastIndexOf
- index of last occurrence
-
A Complete ArrayList Program
- stop words
-
Adding to and Removing from an ArrayList
- intuitive way: adding from backwards
-
Using the For-Each loop
- sequential problem
- iterate and modify cannot happen concurrently
- ConcurrentModificationException
-
wrapper classes
- Arraylist<object type>
-
wrapper class
- wraps(stores) primitive as an obj
- wrap: Integer y = new Integer(38);
- Unwrap: int number = y.intValue();
-
boxing
- automatic conversion from primitive to object
- int to Integer
-
unboxing
- wrapped obj to primitive data
- Integer to int