Chapter 101 Dynamic Data Structures and Generics Chapter 10.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

JAVA & Linked List Implementation
Review Generics and the ArrayList Class
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ITEC200 Week04 Lists and the Collection Interface.
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
M180: Data Structures & Algorithms in Java
Generics and the ArrayList Class
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Vectors l Linked Data Structures Dynamic Data Structures.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
Chapter 11 Arrays Continued
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Java SE 8 for Programmers, Third Edition
Chapter 11. Multidimensional Arrays and Vectors Multidimensional Arrays Vectors Computer Programming with JAVA.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Lecturer: Dr. AJ Bieszczad Chapter 10 COMP 150: Introduction to Object-Oriented Programming 10-1 l Vectors l Linked Data Structures Dynamic Data Structures.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
CSE 1201 Object Oriented Programming ArrayList 1.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Multidimensional Arrays l Vectors Multidimensional Arrays.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Vectors l Linked Data Structures Dynamic Data Structures.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Building Java Programs Generics, hashing reading: 18.1.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Vectors l Linked Data Structures Dynamic Data Structures.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
CMSC 202 ArrayList Aug 9, 2007.
Java Programming: Guided Learning with Early Objects
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Vectors Linked Data Structures
CMSC 202 ArrayList Aug 9, 2007.
Dynamic Data Structures and Generics
Object Oriented Programming in java
CMSC 202 ArrayList Aug 9, 2007.
Dynamic Data Structures and Generics
Review: libraries and packages
Presentation transcript:

Chapter 101 Dynamic Data Structures and Generics Chapter 10

2 Reminders Project 6 due Nov 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

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.

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.)

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

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.

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 ***

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”

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”

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.

Chapter 1011

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.

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.

Chapter 1014 Linked Lists, cont.

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

Chapter 1016

Chapter 1017 Moving Down a Linked List

Chapter 1018 Adding a Node at the Start

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

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

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.

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.

Chapter 1023

Chapter 1024

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.

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];

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.

Chapter 1028 Advancing to the Next Node

Chapter 1029 Adding a Node

Chapter 1030 Deleting a Node

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.

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;... }

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;... }

Chapter 1034 Variations on a Linked List, cont.

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.

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.

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.

Chapter 1038 class Sample Generic Basics, cont.

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.

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

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

Chapter 1042

Chapter 1043

Chapter 1044

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

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

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