You are on page 1of 8

4/19/2015

Array-Based Lists

Collections and ArrayIntList


Reading: RS Ch. 15

slides created by Marty Stepp


http://www.cs.washington.edu/143/
Modified by Sarah Heckman

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()
...

– The list's size will be the number of elements added to it so far.


– How will the list be used?...
3

Java collections framework

2
4/19/2015

Generic interface (16.5)


// Represents a list of values.
public interface List<E> {
public void add(E value);
public void add(int index, E value);
public E get(int index);
public int indexOf(E value);
public boolean isEmpty();
public void remove(int index);
public void set(int index, E value);
public int size();
}

public class ArrayList<E> implements List<E> { ...


public class LinkedList<E> implements List<E> { ...

Java collections framework

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);

ArrayList<Integer> list2 = new


ArrayListTest.java (client test)
ArrayList<Integer>();
list2.add(-3); public class ArrayListTest {
list2.add(98); @Test
list2.add(2); public void testAdd() {}
} }
}

index 0 1 2 ... 9 index 0 1 2 ... 9


value -3 98 2 ... 0 value 17 22 0 ... 0
size 3 size 2 8

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);
}

public ArrayList(int capacity) {


if (capacity < 0) {
throw new IllegalArgumentException();
}
list = new E[capacity]; //illegal!
size = 0;
}

CSC216: Programming Concepts – Java 
© 2010 Sarah Heckman

Generics and arrays (15.4)


public class Foo<E> {
private E myField; // ok

public void method1(E param) {


myField = new E(); // error
E[] a = new E[10]; // error
}
}

– You cannot create objects or arrays of a parameterized type.

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

Comparing generic objects


public class ArrayList<E> {
...
public int indexOf(E value) {
for (int i = 0; i < size; i++) {
// if (list[i] == value) {
if (list[i].equals(value)) {
return i;
}
}
return -1;
}
}

– When testing objects of type E for equality, must use equals


14

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

You might also like