List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

30
List Interface Click icon to add picture CS340 1

Transcript of List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

Page 1: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

1

List Interface

Click icon to add picture

CS340

Page 2: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 2

java.util.List Interface and its Implementers

Page 3: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 3

Generic Collections The statement

List<String> myList = new ArrayList<String>();

uses generic collections or generics Only references of type String can be stored in the

list String in this statement is called a type parameter Sets the data type of all objects stored in a collection

Page 4: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 4

Generic Collections (cont.) The general declaration for generic collection is

CollectionClassName<E> variable = new CollectionClassName<E>(); The <E> indicates a type parameter However, primitive types will be autoboxed:

ArrayList<Integer> myList = new ArrayList<Integer>(); myList.add(new Integer(3)); // okmyList.add(3); // also ok! 3 is automatically wrapped in an Integer objectmyList.add(new String("Hello")); // generates a type incompatability error

Page 5: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 5

Why Use Generic Collections?• Better type-checking: catch more errors, catch them

earlier• Documents intent• Avoids the need to downcast from Object

Page 6: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

6

Single-Linked Lists

Click icon to add picture

CS340

Page 7: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 7

Single-Linked Lists• Useful for

• inserting • removing

at arbitrary locations

• ArrayList: add and remove methods operate in linear O(n) time

• A linked list can add and remove elements at a known location in O(1) time

Page 8: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 8

A List Node• A node can contain:

• a data item• one or more links

• A link is a reference to a list node• In our list the node contains:

• a data field named data of type E• a reference to the next node, named next

Page 9: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 9

List Nodes for Single-Linked Listsprivate static class Node<E> { private E data; private Node<E> next; /** Creates a new node with a null next field @param dataItem The data stored */ private Node(E dataItem) { data = dataItem; next = null; }// continued on next page

static indicates that the Node<E> class will not reference its outer class

Static inner classes are also called nested classes

Page 10: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 10

List Nodes for Single-Linked Lists (cont.)

/** Creates a new node that references another node @param dataItem The data stored @param nodeRef The node referenced by new node */ private Node(E dataItem, Node<E> nodeRef) { data = dataItem; next = nodeRef; }}

Page 11: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 11

Connecting Nodes

Page 12: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 12

Connecting Nodes (cont.)Node<String> tom = new Node<String>("Tom");Node<String> dick = new Node<String>("Dick");Node<String> harry = new Node<String>("Harry");Node<String> sam = new Node<String>("Sam");

tom.next = dick;dick.next = harry;harry.next = sam;

Page 13: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 13

A Single-Linked List Class A SingleLinkedList object has a data field head,

which references the first list node

public class SingleLinkedList<E> { private Node<E> head = null; private int size = 0; ...}

Page 14: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 14

SLList: An Example List

head =

SLList<String>

next =data = "Tom"

Node<String>

next =data = “Helen"

Node<String>

Page 15: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 15

Implementing SLList.addFirst(E item)

head =

SLList<String>

next =data = "Tom"

Node<String>

next =data = “Helen"

Node<String>

next =data = "Ann"

Node<String>

The element added to the list

Page 16: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 16

Implementing SLList.addFirst(E item) (cont.)

private void addFirst (E item) { Node<E> temp = new Node<E>(item, head); head = temp; size++;

}

or, more simply ...

private void addFirst (E item) { head = new Node<E>(item, head); size++;}

This works even if head is null

Page 17: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 17

Implementing addAfter(Node<E> node, E item)

head =

SLList<String>

next =data = "Tom"

Node<String>

next =data = “Helen"

Node<String>

next =data = "Ann"

Node<String>

The element added to the list

Page 18: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 18

Implementing addAfter(Node<E> node, E item) (cont.)private void addAfter (Node<E> node, E item) { Node<E> temp = new Node<E>(item, node.next); node.next = temp; size++;

}

or, more simply ...

private void addAfter (Node<E> node, E item) { node.next = new Node<E>(item, node.next); size++;

}

We declare this method private since it should

not be called from outside the class

Page 19: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 19

Implementing removeAfter(Node<E> node)

head =

SLList<String>

next =data = "Tom"

Node<String>

next =data = “Helen"

Node<String>

next =data = "Ann"

Node<String>

temp

The Node parameter

Page 20: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 20

Implementing removeAfter(Node<E> node) (cont.)private E removeAfter (Node<E> node) { Node<E> temp = node.next; if (temp != null) { node.next = temp.next; size--; return temp.data; } else { return null; }}

Page 21: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 21

Implementing SLList.removeFirst()

head =

SLList<String>

next =data = "Tom"

Node<String>

next =data = “Helen"

Node<String>

temp

Page 22: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 22

Implementing SLList.removeFirst() (cont.)

private E removeFirst () { Node<E> temp = head; if (head != null) { head = head.next; } if (temp != null) { size--; return temp.data } else { return null; }}

Page 23: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 23

Traversing a Single-Linked List

head =

SLList<String>

next =data = "Tom"

Node<String>

next =data = “Helen"

Node<String>

next =data = "Ann"

null

Node<String>

nodeRef

Do something with nodeRefDo something with nodeRefDo something with nodeRef

Page 24: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 24

Traversing a Single-Linked List (cont.)

toString()can be implemented with a traversal:

public String toString() { Node<String> nodeRef = head; StringBuilder result = new StringBuilder(); while (nodeRef != null) { result.append(nodeRef.data); if (nodeRef.next != null) { result.append(" ==> "); } nodeRef = nodeRef.next; } return result.toString();}

Page 25: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 25

Completing the SingleLinkedList Class

Page 26: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 26

SLList.getNode(int)

Additional helper method:

private Node<E> getNode(int index) { Node<E> node = head; for (int i=0; i<index && node != null; i++) { node = node.next; } return node;}

Page 27: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 27

public E get(int index)public E get (int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node<E> node = getNode(index); return node.data;}

Page 28: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 28

public E set(int index, E newValue)

public E set (int index, E newValue) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node<E> node = getNode(index); E result = node.data; node.data = newValue; return result;}

Page 29: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

29

public void add(int index, E item)

public void add (int index, E item) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if (index == 0) { addFirst(item); } else { Node<E> node = getNode(index-1); addAfter(node, item); }}

Page 30: List Interface CS340 1. java.util.List Interface and its Implementers CS340 2.

CS340 30

public boolean add(E item)

• To add an item to the end of the listpublic boolean add (E item) { add(size, item); return true;

}