Professional Documents
Culture Documents
Array Based List
Array Based List
Array-Based Lists
Lists
• list: a collection storing an ordered sequence of elements,
each accessible by a 0-based index
– a list has a size (number of elements that have been added)
– elements can be added to the front, back, or elsewhere
1
4/19/2015
Example
• Let's write a class that implements a list using an int[]
– We'll call it ArrayIntList
– behavior:
•add(value), add(index, value)
•get(index), set(index, value)
•size()
•remove(index)
•indexOf(value)
•toString()
...
2
4/19/2015
3
4/19/2015
AbstractList
• Provides a partial implementation of an array-based linear data
structure (random-access storage)
• Still need to provide the state:
– Array of objects
– Size
• Unmodifiable list – required for concrete ArrayList
– Implement get(int) and size()
– Not very useful since you can’t add
• Modifiable list – Change the elements
– Override set(int, E)
• Variable size list – Grow the array automatically
– Override add(int, E) and remove(int)
7
Client programs
• ArrayList.java is not, by itself, a runnable program.
– ArrayList can be used by client programs generically.
Main.java (client program) ArrayList.java (library class)
public class Main { public class ArrayList <E>{
public static void main(String[] args) { private E[] list;
ArrayList<Integer> list1 = new private int size;
ArrayList<Integer>(); ...
list1.add(17);
}
list1.add(22);
4
4/19/2015
Pros/cons of ArrayIntList
• pro (benefits)
– simple syntax
– don't have to keep track of array contents, size, or capacity
– has powerful methods (indexOf, add, remove, toString)
• con (drawbacks)
– ArrayIntList only works for ints (arrays can be any type)
– syntax is different to learn and use
Generics in Java
• Provide a type to collection classes (or any classes)
– An ArrayList of Strings or Books or Beers
• Our own classes can provide generics too
– Use E to represent the element/type the user provides
• Start making ArrayIntList generic by changing
– ArrayIntList to ArrayList<E>
– int to E when referring to an element of the list, not the
size, indexes, and capacity
CSC216: Programming Concepts – Java
© 2010 Sarah Heckman
5
4/19/2015
Constructors in Generic Classes
• Don’t use the generic E when naming a constructor
public ArrayList() {
this(DEFAULT_CAPACITY);
}
CSC216: Programming Concepts – Java
© 2010 Sarah Heckman
12
6
4/19/2015
Generics/arrays, fixed
public class Foo<E> {
private E myField; // ok
@SuppressWarning(“unchecked”)
public void method1(E param) {
myField = param; // ok
E[] a2 = (E[]) (new Object[10]); // ok
}
}
– But you can create variables of that type, accept them as
parameters, return them, or create arrays by casting Object[].
– Ignore the Cast warning generated by Java since you cannot
create a generic array
• Use annotation @SuppressWarnings(“unchecked”) at top of
method.
13
7
4/19/2015
Memory Allocation
• If you remove an element, then there is still data in array
locations outside the size of the array (but within the capacity)
index 0 1 2 3 4 5 6 7 8 9
value 3 8 7 5 12 12 n n n n
size 5
• If we’re using generics, our list contains Integers – objects
• Java’s garbage collector finds objects that are no longer used
(referenced) and destroys them
– Our array list may interfere with this. The object referenced by index 5 is
no longer used, but still referenced. Therefore, no garbage collection.
– Need to explicitly set the array element back to null (removes reference)
– When could this be a problem?
CSC216: Programming Concepts – Java
© 2010 Sarah Heckman