Download presentation
Presentation is loading. Please wait.
Published byClarence Weaver Modified over 9 years ago
1
List Interface CS340 1
2
java.util.List Interface and its Implementers CS340 2
3
Generic Collections The statement List myList = new ArrayList (); 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 CS340 3
4
Generic Collections (cont.) The general declaration for generic collection is CollectionClassName variable = new CollectionClassName (); The indicates a type parameter However, primitive types will be autoboxed: ArrayList myList = new ArrayList (); myList.add(new Integer(3)); // ok myList.add(3); // also ok! 3 is automatically wrapped in an Integer object myList.add(new String("Hello")); // generates a type incompatability error CS340 4
5
Why Use Generic Collections? Better type-checking: catch more errors, catch them earlier Documents intent Avoids the need to downcast from Object CS340 5
6
Single-Linked Lists CS340 6
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 CS340 7
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 CS340 8
9
List Nodes for Single-Linked Lists private static class Node { private E data; private Node 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 CS340 9 static indicates that the Node class will not reference its outer class Static inner classes are also called nested classes
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 nodeRef) { data = dataItem; next = nodeRef; } CS340 10
11
Connecting Nodes CS340 11
12
Connecting Nodes (cont.) Node tom = new Node ("Tom"); Node dick = new Node ("Dick"); Node harry = new Node ("Harry"); Node sam = new Node ("Sam"); tom.next = dick; dick.next = harry; harry.next = sam; CS340 12
13
A Single-Linked List Class A SingleLinkedList object has a data field head, which references the first list node public class SingleLinkedList { private Node head = null; private int size = 0;... } CS340 13
14
SLList : An Example List CS340 14 head = SLList next = data = "Tom" Node next = data = “Helen" Node
15
Implementing SLList.addFirst(E item) CS340 15 head = SLList next = data = "Tom" Node next = data = “Helen" Node next = data = "Ann" Node The element added to the list
16
Implementing SLList.addFirst(E item) (cont.) private void addFirst (E item) { Node temp = new Node (item, head); head = temp; size++; } or, more simply... private void addFirst (E item) { head = new Node (item, head); size++; } This works even if head is null CS340 16
17
Implementing addAfter(Node node, E item) CS340 17 head = SLList next = data = "Tom" Node next = data = “Helen" Node next = data = "Ann" Node The element added to the list
18
Implementing addAfter(Node node, E item) (cont.) private void addAfter (Node node, E item) { Node temp = new Node (item, node.next); node.next = temp; size++; } or, more simply... private void addAfter (Node node, E item) { node.next = new Node (item, node.next); size++; } CS340 18 We declare this method private since it should not be called from outside the class
19
Implementing removeAfter(Node node) CS340 19 head = SLList next = data = "Tom" Node next = data = “Helen" Node next = data = "Ann" Node temp The Node parameter
20
Implementing removeAfter(Node node) (cont.) private E removeAfter (Node node) { Node temp = node.next; if (temp != null) { node.next = temp.next; size--; return temp.data; } else { return null; } CS340 20
21
Implementing SLList.removeFirst() CS340 21 head = SLList next = data = "Tom" Node next = data = “Helen" Node temp
22
Implementing SLList.removeFirst() (cont.) private E removeFirst () { Node temp = head; if (head != null) { head = head.next; } if (temp != null) { size--; return temp.data } else { return null; } CS340 22
23
Traversing a Single-Linked List CS340 23 head = SLList next = data = "Tom" Node next = data = “Helen" Node next = data = "Ann" null Node nodeRef Do something with nodeRef
24
Traversing a Single-Linked List (cont.) toString() can be implemented with a traversal: public String toString() { Node 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(); } CS340 24
25
Completing the SingleLinkedList Class CS340 25
26
SLList.getNode(int) Additional helper method: private Node getNode(int index) { Node node = head; for (int i=0; i<index && node != null; i++) { node = node.next; } return node; } CS340 26
27
public E get(int index) public E get (int index) { if (index = size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node node = getNode(index); return node.data; } CS340 27
28
public E set(int index, E newValue) public E set (int index, E newValue) { if (index = size) { throw new IndexOutOfBoundsException(Integer.toString(ind ex)); } Node node = getNode(index); E result = node.data; node.data = newValue; return result; } CS340 28
29
public void add(int index, E item) public void add (int index, E item) { if (index size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if (index == 0) { addFirst(item); } else { Node node = getNode(index-1); addAfter(node, item); } 29
30
public boolean add(E item) To add an item to the end of the list public boolean add (E item) { add(size, item); return true; } CS340 30
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.