Presentation is loading. Please wait.

Presentation is loading. Please wait.

John Hurley Cal State LA

Similar presentations


Presentation on theme: "John Hurley Cal State LA"— Presentation transcript:

1 John Hurley Cal State LA
CS203 Lecture 6 John Hurley Cal State LA

2 Java Collections Framework
A collection is a container object that holds a group of objects, often referred to as elements. The Java Collections Framework supports three types of collections, named sets, lists, and maps.

3 The Collection Interface
The Collection interface is the root interface for manipulating a collection of objects.

4 The List Interface, cont.

5 ArrayList and LinkedList
Like an array, a list stores elements in a sequential order, and allows the user to specify where the element is stored. The user can access the elements by index. Recall that an array has a fixed length. An array is suitable if your application does not require insertion or deletion of elements. A list can grow or shrink dynamically. ArrayList and LinkedList are the most common concrete implementations of the List interface. If you need to support random access through an index without inserting or removing elements from any place other than the end, ArrayList offers the most efficient collection. ArrayList is implemented using an underlying array that begins with a default size. If the list outgrows the array, a new array must be created and the contents of the old one copied. Inserting values at arbitrary spots in the list involves moving all the subsequent elements. If your application requires the insertion or deletion of elements from arbitrary locations in the list, use a LinkedList.

6 java.util.ArrayList

7 java.util.LinkedList LinkedList implements a doubly-linked list. It implements the List interface.

8 Linked Lists A linked list is a data structure consisting of a group of nodes which represent a sequence of values of the parameterizing type. In the simplest form of linked list, each node is composed of a datum and a reference (in other words, a link) to the next node in the sequence. This is called a singly linked list. We will learn more about how linked lists are implemented later; here we will use the Java Collections Framework LinkedList<E> class.

9 Linked Lists In a doubly linked list, each node contains a reference to the next node and a reference to the previous node. Either a singly or double linked list may be circular; that is, last node points to the first, and, in a circular doubly-linked list, the first node has a reference to the last Since the items may not be stored in contiguous memory, the JVM traverses the list by following the links. The additional links in these types of linked lists make this process more efficient. Diagrams from Wikipedia:

10 Linked Lists Adding and removing elements from any spot in a linked list involves changing, at most, one reference in each of two existing elements and setting, at most, two references in a new element. Compare this to the expense of moving all the elements. In an array list or periodically creating a new array and copying all the elements from an old one. Add a node to a singly-linked list: Inserting a node into a doubly linked list just involves also changing the reference from the subsequent element and adding one from the new element back to the previous one

11 Linked Lists Delete a node from a singly-linked list:

12 Linked Lists Based on the info above, you might expect linked lists to be much more efficient than array lists. However, data in a linked list is accessed sequentially; we start at the head of the list and follow the references. This is called traversing the list. node = list.firstNode while node not null (do something with node.data) node = node.next Source for pseudocode: Wikipedia This may make a linked list less efficient than an array list for operations that require a great deal of traversal, because accessing an array element by index is very efficiently implemented. Also, memory is allocated on the fly when a node is added. This makes it more likely that different nodes will be far apart in memory, which sometimes makes it more expensive to traverse the list.

13 Linked Lists Course page has link to listcompare package

14 I/O Expense This is a good time to point out that I/O is very expensive. Rerun the List Comparer code above with System.out.println statements inside the loops in the add and get methods.

15 Lower-Level Implementation of Lists
You will probably never need to code a List yourself; the ones built into the JDK are good enough for nearly all cases. However, a lab later in the course will involve coding a more-complex data structure. The course web page contains a link to a code package with a partial implementation of a linked list.

16 Iterators An iterator is an object that provides methods for traversing the elements of a collection. Even with Lists, which are not hard to manipulate using loops, iterators sometimes provide easier ways to traverse data structures In many cases, the enhanced for loop is just as convenient as an iterator.

17 The List Iterator

18 Iterator public static void main(String[] args) {
List<String> names = new LinkedList<String>(); names.add("John"); names.add("Paul"); names.add("George"); names.add("Ringo"); String currName; ListIterator<String> namesIterator = names.listIterator(); while(namesIterator.hasNext()){ currName = namesIterator.next(); System.out.println(currName + " " + (currName.compareTo("Mick") < 0? "earlier than Mick": "not earlier than Mick")); }

19 More on Desk Tracing Do a desk trace of this recursive code, then run the code and see if the actual output matches: package demos; public class RecursiveTracer { final int MAX = 4; public static void main(String[] args) { RecursiveTracer t = new RecursiveTracer(); t.trace(0); } public void trace(int curr) { printStack(curr); if(curr < MAX) trace(++curr); public void printStack(int curr){ for(int counter = curr; counter >= 0; counter--){ System.out.printf(" \n|counter = %2d |\n", counter); System.out.println(" \n\n");


Download ppt "John Hurley Cal State LA"

Similar presentations


Ads by Google