Download presentation

Presentation is loading. Please wait.

1
Chapter 101 Dynamic Data Structures and Generics Chapter 10

2
2 Reminders Project 6 due Nov 03 @ 10:30 pm Project 5 grades released (or by tonight): regrade requests due by next Friday Exam 2: handed back next week, solution discussed next week

3
Chapter 103 Introduction A data structure is a construct used to organize data in a specific way. An array is a static data structure. Dynamic data structures can grow and shrink while a program is running. Vectors and linked data structures are dynamic.

4
Chapter 104 Introduction to Vectors, cont. Vectors serve the same purposes as arrays, but can change in length while a program is running. This added flexibility comes at a price: –Vectors are less efficient than arrays. –The base type of a vector must be a class type rather than a primitive type. (Automatic boxing and unboxing make this requirement less significant than it used to be.)

5
Chapter 105 Using Vectors The definition of class Vector must be imported. import java.util.*; to create and name a vector Vector v = new Vector (20); –The vector v stores objects of class String and has an initial capacity of 20. Don’t need to specify the class type Vector v = new Vector(20); - Can store any kind of object

6
Chapter 106 Using Vectors, cont. –When more capacity is needed, the system allocates more memory automatically. –If the initial capacity was sufficient, the code is more efficient. In this example, the base type is type String. –Any class can be used as the base type. –But, wrapper classes MUST be used for primitive types.

7
Chapter 107 Adding, Getting, and Setting Values to add an element v.addElement(“Hello!”); to get the value of an element String temp = v.elementAt(index); to change the value of an existing element v.setElementAt(“Hi, Mom!”, index); *** element must already exist to use setElementAt ***

8
Chapter 108 addElement vs. insertElementAt Remember that add element always adds an element at the end of the Vector. If the Vector v currently contains: After v.addElement(“E”) : If we had done v.insertElementAt(“E”,2) instead of the addElement line above: Finally, if we had done v.insertElementAt(“E”,4) instead of the two lines above, it would be the same as calling v.addElement(“E”) : “A”“B”“C”“D” size = 4 “A”“B”“C”“D” size = 5 “E” “A”“B”“E”“C” size = 5 “D” “A”“B”“C”“D” size = 5 “E”

9
Chapter 109 setElementAt vs. insertElementAt setElementAt changes an existing element while insertElementAt inserts a new element. The index parameter of setElementAt must be less than the size of the vector. Example: Our original Vector v : After v.setElementAt(“E”,2) : After v.insertElementAt(“E”,2) : (Instead of line above) “A”“B”“C”“D” size = 4 “A”“B”“E”“D” size = 4 “A”“B”“E”“C” size = 5 “D”

10
Chapter 1010 Size and Indices to learn the maximum capacity of the vector int maxSize = v.capacity(); To learn the size of the vector int howMany = v.size(); The indices range from 0 to v.size()-1.

11
Chapter 1011

12
Chapter 1012 Introduction to Linked Data Structures A linked data structure is a collection of objects (called nodes), each containing data and a (potential) reference to (at least) one other node.

13
Chapter 1013 Linked Lists The predefined LinkedList class is part of the java.util package. Nevertheless, to learn how linked data structures work, we’ll construct a simplified example of a linked list.

14
Chapter 1014 Linked Lists, cont.

15
Chapter 1015 Linked Lists, cont. Each node is an object of a class that has (at least) two instance variables: –the data –the link.

16
Chapter 1016

17
Chapter 1017 Moving Down a Linked List

18
Chapter 1018 Adding a Node at the Start

19
Chapter 1019 Inner Classes An inner class is a class defined within another class.

20
Chapter 1020 Defining an Inner Class public class OuterClass { OuterClass_Instance_Variables OuterClass_Methods private class InnerClass { InnerClass_Instance_Variables InnerClass_Methods }

21
Chapter 1021 Access to Members The inner and outer classes’ methods have access to each other’s methods and instance variables, even when they are declared private.

22
Chapter 1022 Node Inner Classes By making the node class an inner class, data structure classes become self-contained. Further, the accessor and mutator methods of the inner class can be eliminated since instance variables of an inner class are accessible directly.

23
Chapter 1023

24
Chapter 1024

25
Chapter 1025 Iterators With a collection of objects, such as the nodes of a linked list, we often need to “step through” all the objects to perform some action on each object. An iterator allows us to “step through” a collection of objects.

26
Chapter 1026 Iterators, cont. The loop control variable of a for loop functions as an iterator for an array. for (int i = 0; i < a.length, i++) process a[i];

27
Chapter 1027 Iterators, cont. Similarly, an instance variable capable of referencing a node, can serve the same purpose as the loop control variable in a for loop. Another instance variable capable of referencing a node can “follow behind” to provide access to the previous node.

28
Chapter 1028 Advancing to the Next Node

29
Chapter 1029 Adding a Node

30
Chapter 1030 Deleting a Node

31
Chapter 1031 Variations on a Linked List A reference to the last node in a linked list can be useful. … public ListNode head; public ListNode tail; … A linked list can contain (or reference) any kind of data.

32
Chapter 1032 Variations on a Linked List, cont. A linked list can contain different kinds of objects private class ListNode { private Object data; private ListNode link;... }

33
Chapter 1033 Variations on a Linked List, cont. An additional reference can be added to reference the previous node, producing a doubly-linked list. private class ListNode { private Object data; private ListNode next; private ListNode previous;... }

34
Chapter 1034 Variations on a Linked List, cont.

35
Chapter 1035 Variations on a Linked List, cont. The last node in a singly-linked list can reference the first node, producing a circularly-linked list. The last node in a doubly-linked list can reference the first node with its next reference, and the first node can reference the last node with its previous reference, producing a doubly-circularly-linked list.

36
Chapter 1036 Introduction to Generics Java 5.0 allows definitions, called generics, that include parameters for types. Generics can be subtle and full of pitfalls. We provide an introduction to generics. Serious programming with generics is presented in more advanced texts.

37
Chapter 1037 Generic Basics Classes and methods can have a type parameter. Any class type can be substituted for the type parameter, producing a specific class type or method.

38
Chapter 1038 class Sample Generic Basics, cont.

39
Chapter 1039 Generic Basics, cont. A class definition with a type parameter is stored in a file and compiled just like any other class. When used in code a class type must be specified so that it can be substituted for the type parameter.

40
Chapter 1040 Generic Basics, cont. example Sample o1 = new Sample (); o1.setData(“Hello”); Sample o2 = new Sample (); Species s = new Species(); o2.setData(s);

41
Chapter 1041 Generic Basics, cont. You cannot substitute a primitive type for a type parameter. You must instead use a class type.

42
Chapter 1042

43
Chapter 1043

44
Chapter 1044

45
Chapter 1045 Generic Constructor The class name in a parameterized class definition has a type parameter attached. But, a generic constructor name has no type parameter and the type parameter is not used in the heading of the constructor definition. public LinkedList() not public LinkedList () // ILLEGAL

46
Chapter 1046 Summary You have become familiar with vectors. You have learned about linked data structures in Java. You have learned how to manipulate linked lists. You have learned to use inner classes in defining linked data structures

47
Chapter 1047 Summary, cont. You have learned about iterators. You have learned about generics (parameters for types).

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google