ArrayLists Using arrays to store data Arrays store multiple values - - PowerPoint PPT Presentation

arraylists using arrays to store data
SMART_READER_LITE
LIVE PREVIEW

ArrayLists Using arrays to store data Arrays store multiple values - - PowerPoint PPT Presentation

ArrayLists 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 Dont


slide-1
SLIDE 1

ArrayLists

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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 1 2 3 4 5 6 ... 98 99 value 17 932085

  • 3

100 3 ... size 5

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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;

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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)

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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) { ... }

slide-12
SLIDE 12

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 1 2 3 4 5 6 7 8 9 value 3 8 9 7 5 12 size 6 index 1 2 3 4 5 6 7 8 9 value 3 8 9 7 5 12 42 size 7

slide-13
SLIDE 13

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 1 2 3 4 5 6 7 8 9 value 3 8 9 7 5 12 size 6 index 1 2 3 4 5 6 7 8 9 value 3 8 9 42 7 5 12 size 7

slide-14
SLIDE 14

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 1 2 3 4 5 6 7 8 9 value 3 8 9 42 7 5 12 size 7

slide-15
SLIDE 15

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 1 2 3 4 5 6 7 8 9 value 3 8 9 7 5 12 size 6 index 1 2 3 4 5 6 7 8 9 value 3 8 7 5 12 size 5

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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;

slide-18
SLIDE 18

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,

  • r that it is a generic class.

ArrayList<String> names = new ArrayList<String>(); names.add(”Asa"); names.add(”Nathan");

slide-19
SLIDE 19

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]"

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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.

slide-22
SLIDE 22

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 1 2 3 4 5 value "she" "sells" "seashells" "by" "the" "seashore" size 6

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

slide-26
SLIDE 26

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