Null
For reference variables may not be pointing at any object
- Not an object
- A reserved literal value for reference type
Just like "true" or "false" for boolean type
- For reference types, default value is "null"
Local reference variables are not automatically initialized to "null"
- You must initialize them by either calling a constructor or setting them to null
- In fact, Java assigns no default value to a local variable
- FYI: Empty String is actually a String object, not null
Note: Be aware of NullPointerException
java.util.ArrayList
Implements an expandable array
- Internally, maintains an array
- No direct access to the array itself (protect data)
- Instead, it provides methods to manipulate the array
- When the array is full, it creates a new larger array and copies the data from old (smaller) array into the new (larger) array
Automatically handles issues relating to length
1. import java.util.ArrayList
2. Allocation of space: List<String> a = new ArrayList<String>();
3. Appending (adding to the end): String item = ...; a.add(item);
4. Inserting before the first element: a.add(0, item);
5. For each
6. Deleting: a.remove(i);
7. Searching: int i = a.indexOf(item); a.contains(item);
ArrayList Manages an Array
Makes an internal array of length 10 - this is the default capacity
Maintains a separate count of the number of elements referenced by the array (the user's perception of size)
If adding an element exceeds the capacity, allocate a new array that is 50% bigger and copy data from the old array into (the beginning of) the new array
java.util.LinkedList
Elements are referenced by Node objects
To add an element, a new Node object is allocated and inserted (using links in the chain) at the beginning, end or middle of the list
O(1) means an operation take time less than c
O(log n) means an operation take time less than c * log n
O(n) means an operation take time less than c * n
O(n log n) means an operation take time less than c * n * log n
O(n^2) means an operation take time less than c * n^2
* n is the number of elements and c is some constant
The List Interface
Specifies common methods that must be implemented by all lists
Implemented by ArrayList, Vector, LinkedList, etc.
Java Interfaces
A Java interface allows you to specify methods that must be implemented by a class
You can then use references to the interface, knowing the methods will be available, even though you don't know the specific class used
An Interface reference variable can reference any object that implements that interface regardless of its class type
Generics in Java
Java allows you to specify a class that manipulates instances of some type <T>
- Using type parameters
Complier now can check:
- When inserting objects of the wrong type
- Incorrect return type when getting an element
- Gives compile time error/warning messages
No cast is required for calling get method
Makes your program safer and easier to read
Before Generics (In Java 4 and before)
The Collections Framework stored Objects
You could put any object into a List
When you took something out of a List...
- You needed to cast it back into your type
- You could check types using instanceof operator
Hash Functions
Convert (compress) data into a number (usually an int)
The goal: Two different inputs should generally (and ideally) have two different outputs
java.util.HashMap
Elements (values) are accessed by keys (not index)
- It maps Keys to Values.
Fast to insert, delete and lookup
- But no order of the elements
Keep enough space
If array gets too full, reallocate the array and rehash everything
Performance Comparisons
Array/ArrayList LinkedList HashSet/HashMap
Append After Last O(1) O(1) O(1)
Insert Before First O(n) O(1) O(1)
Lookup by Position O(1) O(n) N/A
Lookup by Value O(n) O(n) O(1) (by key)
Remove Last O(1) O(1) O(1)
Remove First O(n) O(1) O(1)
Question
Among these data structures, which would be better if not best:
- Looking up student records by andrew id over and over? HashMap
- Managing waitlist for a course?
Maintaining the order Array/ArrayList
Adding and deleting students at both ends LinkedList
java.util.Comparator
Pass a Comparator to sort with an alternative ordering
It's a Java Interface
- Create a class to implement it (implements Comparator<T>)
- Need to implement compare(T a, T b) method
Negative if a comes before b
0 if they are identical
Positive otherwise
Autoboxing
Writing the code to put your ints in Integers is a hassle
- Same for other primitives
In Java 5, Java will automatically convert between primitives and their Object wrapper classes
- When passing parameters or returning values
- In assignment or math expressions
ArrayList<Integer>
Just declare ArrayList<Integer>
Put in and take out ints
Autoboxing automatically does the conversions
Sample Final Exam Questions
Compare the use of ArrayLists and LinkedLists? What are the advantages of each?
ArrayList: Lookup by Position
LinkedList: Insert Before First and Remove First
What is a comparator?
It's a Java Interface
- Create a class to implement it (implements Comparator<T>)
- Need to implement compare(T a, T b) method
Negative if a comes before b
0 if they are identical
Positive otherwise
What is autoboxing? Why is it useful in Java?
Writing the code to put your ints in Integers is a hassle
- Same for other primitives
In Java 5, Java will automatically convert between primitives and their Object wrapper classes
- When passing parameters or returning values
- In assignment or math expressions
What are Java Generics? What are the advantages of using generic classes? What did Java programmers do before we had generic classes?
Generics in Java
Java allows you to specify a class that manipulates instances of some type <T>
- Using type parameters
Complier now can check:
- When inserting objects of the wrong type
- Incorrect return type when getting an element
- Gives compile time error/warning messages
No cast is required for calling get method
Makes your program safer and easier to read
Before Generics (In Java 4 and before)
The Collections Framework stored Objects
You could put any object into a List
When you took something out of a List...
- You needed to cast it back into your type
- You could check types using instanceof operator