Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 101 Dynamic Data Structures and Generics Chapter 10.

Similar presentations


Presentation on theme: "Chapter 101 Dynamic Data Structures and Generics Chapter 10."— Presentation transcript:

1 Chapter 101 Dynamic Data Structures and Generics Chapter 10

2 2 Dynamic Data Structures Motivation – a dynamically changing (in size) collection –Arrays – collections, but static in size Use arrays when the collection is fixed in size, use a dynamic data structure when objects will be added/removed from the collection

3 Chapter 103 Dynamic Data Structures Many found in the java.util package –Vector –LinkedList –ArrayList Differ by organization, making performance issues important in selecting the best structure to use

4 Chapter 104 Vectors Work like resizable arrays Indexing: elementAt(int) Insertion: add(…) –Many overloaded forms Length: size() Many other useful methods – consult the API! Vector v = new Vector (5); v.add(“Project”); v.add(“ 4”); v.add(“ was”); v.add(“ the”); v.add(“ best!”); int size = v.size(); for (int k=0; k<size; k++) { System.out.print(v.elementAt(k)); }

5 Chapter 105 Linked Lists Another dynamic data structure Think of as a chain of items strung together Two primary items in a list node –Data –Next pointer (also known as a link) Java has a LinkedList class too! (But for practice, we will learn to make our own)

6 Chapter 106 Linked Lists Like Vector, LinkedList is a Java Collection Many similar functions, but some different ones as well Can use new “for each” loop –This iterates through our list –Can also use Iterator interface LinkedList l = new LinkedList (); l.add(“Project”); l.add(“ 4”); l.add(“ was”); l.add(“ the”); l.add(“ best!”); int size = v.size(); for (String s : l) { System.out.print(s); }

7 Chapter 107 Iterators All collections can iterate through their objects –Use Iterator interface 2 primary methods – next(), hasNext() Java collections have an iterator() method –Use foreach loop Internal iterators – looping done inside Collection External iterators – looping done with external variables

8 Chapter 108 Our Own Linked List A list of Strings This class is not flexible! –Can only store Strings inside this list public class Node { private String item; private Node next; public Node() { item = null; next = null; } public Node(String item, Node next) { this.item = item; this.next = next; } public String getItem() { return item; } … }

9 Chapter 109 Our Own Generic Linked List Use parameterized type –Typically represented by a letter (like T) –Allows us to store any type of object inside the node T can be a String T can be a Vector T cannot be both at once –Cannot store primitives, but Java autoboxes public class Node { private T item; private Node next; public Node() { item = null; next = null; } public Node(T item, Node next) { this.item = item; this.next = next; } public T getItem() { return item; } … }

10 Chapter 1010 Our Own Generic Linked List Many times, easy to use with a wrapper class around the list –Structure exactly like that of Project 6 MyLinkedList class –Can change inside functionality, but outside world remains the same Node class is not needed outside the class! –Make it an inner class public class MyList { private Node head; public MyList() { head = null; } public MyList(T item) { head = new Node (item, null); } … private class Node { … }

11 Chapter 1011 Variations to Linked Lists Doubly linked list –Add a second previous pointer to link backwards as well Circularly linked list –Link tail pointer to head to form a ring


Download ppt "Chapter 101 Dynamic Data Structures and Generics Chapter 10."

Similar presentations


Ads by Google