ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in...

26
ArrayLists

Transcript of ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in...

Page 1: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

ArrayLists

Page 2: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Using arrays to store data

Arrays store multiple values of the same type

Individual values are referred to by index

Convenient to use

a[i] = x;

x = a[i];

Limitations of arrays

Fixed in size

Don’t indicate which values are valid

Page 3: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Using arrays to store data

int[] nums = new int[100];

int size = 0;

We often need to store an unknown number of

values.

Arrays can be used for this, but we must count the values.

Only the values at indexes [0, size - 1] are relevant.

Need to resize the array to store additional values

index 0 1 2 3 4 5 6 ... 98 99

value 17 932085 -3 100 3 0 0 ... 0 0

size 5

Page 4: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Solving the fixed size – make a Class

public Class IntArray {

private int[] data;

public IntArray (int initialSize) {

data = new int[initialSize];

}

public int get (int index)

return ((index < data.length) ? data[index] : 0);

}

public void set (int index, int val) {

if (index >= data.length)

growArray(index);

data[index] = val;

}

Page 5: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Solving the fixed size – cont

public Class IntArray {

private int[] data;

private void growArray (int index) {

int newSize = ??; // how do we determine this?

int[] newData = new int[newSize];

for (int i = 0; i < data.length; i++)

newData[i] = data[i];

data = newData;

}

Page 6: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Using our new class

IntArray a = new IntArray(10);

int x = a.get(i); // instead of x = a[i];

a.set(i, x); // instead of a[i] = x;

Page 7: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Solving the validity of the entries

Keep track of number of valid entries

Track number of entries in instance variable size

Only values at indices 0 <= i < size are valid

Page 8: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Other possible operations

public static void add(int[] data, int size, int value)

public static void remove(int[] data, int size, int index)

public static void find(int[] data, int size, int value)

public static void print(int[] data, int size)

We could implement these operations as methods that

accept an array and its size along with other parameters.

But since the behavior and data are so closely related, it makes more

sense to put them together into a class.

Objects of such a class can store an array of elements and a size,

and can have methods for manipulating the elements.

Promotes abstraction (hides details of how the list works)

Page 9: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

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 back, or elsewhere

Page 10: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Thought experiment...

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)

...

The list's size will be the number of elements in the list

Page 11: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Using ArrayIntList

construction ArrayIntList list = new ArrayIntList();

storing a value retrieving a value // add at end

list.add(42); int n = list.get(0);

// add in middle

list.add(10,42);

searching for the value 27

if (list.indexOf(27) >= 0) { ... }

Page 12: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Implementing add

Add to end of list is easy; just store element and

increase size

public void add(int value) {

list[size] = value;

size++;

}

list.add(42);

index 0 1 2 3 4 5 6 7 8 9

value 3 8 9 7 5 12 0 0 0 0

size 6

index 0 1 2 3 4 5 6 7 8 9

value 3 8 9 7 5 12 42 0 0 0

size 7

Page 13: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Implementing add (2)

Adding to the middle or front is harder

must shift nearby elements to make room for the new value

list.add(3, 42);

Note: The order in which you traverse the array matters!

index 0 1 2 3 4 5 6 7 8 9

value 3 8 9 7 5 12 0 0 0 0

size 6

index 0 1 2 3 4 5 6 7 8 9

value 3 8 9 42 7 5 12 0 0 0

size 7

Page 14: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Implementing add (2)

public void add(int index, int value) {

for (int i = size; i > index; i--) {

list[i] = list[i - 1];

}

list[index] = value;

size++;

}

list.add(3, 42);

index 0 1 2 3 4 5 6 7 8 9

value 3 8 9 42 7 5 12 0 0 0

size 7

Page 15: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Implementing remove

public void remove(int index) {

for (int i = index; i < size; i++) {

list[i] = list[i + 1];

}

size--;

list[size] = 0; // optional (why?)

}

list.remove(2);

index 0 1 2 3 4 5 6 7 8 9

value 3 8 9 7 5 12 0 0 0 0

size 6

index 0 1 2 3 4 5 6 7 8 9

value 3 8 7 5 12 0 0 0 0 0

size 5

Page 16: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Preconditions

What happens if the client tries to access an element

that is past the size but within the bounds of the array?

Example: list.get(11); on a list of 5 elements, capacity 100

We have not addressed this case yet, one approach is to

ask/assume that the user will not do such a thing.

precondition: Something your method assumes is true

at the start of its execution.

// Returns the element at the given index.

// Precondition: 0 <= index < size

public void get(int index) {

return list[index];

}

Page 17: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Java Collections and ArrayLists

Java includes a large set of powerful collection

classes.

The most basic, ArrayList, is essentially the

same as our ArrayIntList but can store any

type of value.

All collections are in the java.util package.

import java.util.ArrayList;

Page 18: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Type Parameters (Generics)

ArrayList<Type> name = new ArrayList<Type>();

When constructing an ArrayList, you can specify the

type of elements it will contain between < and >.

We say that the ArrayList class accepts a type parameter,

or that it is a generic class.

ArrayList<String> names = new ArrayList<String>();

names.add(”Asa");

names.add(”Nathan");

Page 19: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

ArrayList methods

add(value) appends value at end of list

add(index, value) inserts given value at given index, shifting subsequent values right

clear() removes all elements of the list

indexOf(value) returns first index where given value is found in list (-1 if not found)

get(index) returns the value at given index

remove(index) removes/returns value at given index, shifting subsequent values left

set(index, value) replaces value at given index with given value

size() returns the number of elements in list

toString() returns a string representation of the list such as "[3, 42, -7, 15]"

Page 20: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

ArrayList methods 2

addAll(list)

addAll(index, list) adds all elements from the given list at the end of this list inserts the list at the given index of this list

contains(value) returns true if given value is found somewhere in this list

containsAll(list) returns true if this list contains every element from given list

equals(list) returns true if given other list contains the same elements

remove(value) finds and removes the given value from this list

removeAll(list) removes any elements found in the given list from this list

retainAll(list) removes any elements not found in given list from this list

subList(from, to) returns the sub-portion of the list between indexes from (exclusive) and to (inclusive)

toArray() returns an array of the elements in this list

Page 21: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Learning about classes

The Java API Specification is a huge web page containing

documentation about every Java class and its methods.

The link to the API Specs is on the course web site.

Page 22: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Modifying while looping

Consider the following flawed pseudocode

algorithm to remove plural elements from a list:

removeEndS(list) {

for (int i = 0; i < list.size(); i++) {

get element i;

if it ends with an 's', remove it.

}

}

What does the algorithm do wrong?

index 0 1 2 3 4 5

value "she" "sells" "seashells" "by" "the" "seashore"

size 6

Page 23: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

ArrayList of primitives?

The type you specify when creating an ArrayList

must be an object type; it cannot be a primitive

type.

The following is illegal:

// illegal -- int cannot be a type parameter

ArrayList<int> list = new ArrayList<int>();

But we can still use ArrayList with primitive

types by using special classes called wrapper

classes in their place. ArrayList<Integer> list = new ArrayList<Integer>();

Page 24: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Wrapper classes

A wrapper is an object whose purpose is to hold a primitive value

and to provide more functionality.

Once you construct the list, use it with primitives as normal

(autoboxing):

ArrayList<Double> grades = new ArrayList<Double>();

grades.add(3.2);

grades.add(2.7);

Primitive Type Wrapper Type

int Integer

double Double

char Character

boolean Boolean

Page 25: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Wrapper classes - continued

Autoboxing:

ArrayList<Double> grades = new ArrayList<Double>();

// Autoboxing: create Double from double 3.2

grades.add(3.2);

grades.add(2.7);

double sum = 0.0;

for (int i = 0; i < grades.size(); i++) {

//AutoUNboxing from Double to double

sum += grades.get(i);

}

...

Page 26: ArrayLists - cs.colostate.educs161/Spring12/... · removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given

Looking ahead: Interfaces

An Java interface specifies which public

methods are available to a user

A class implements an interface if it provides

all the methods in the interface

Interfaces allow for a common behavior

amongst classes, eg the Collection

interface is implemented by many classes

(LinkedList, ArrayList...)

ArrayLists implement the Java Collection

Interface. Let's go look on line...