Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Chapter 10 Introduction to Arrays
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
HST 952 Computing for Biomedical Scientists Lecture 8.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
1 Chapter 10 l Vector l Linked Data Structures l interface 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.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Chapter 9 Introduction to Arrays
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
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.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Lecturer: Dr. AJ Bieszczad Chapter 10 COMP 150: Introduction to Object-Oriented Programming 10-1 l Vectors l Linked Data Structures Dynamic Data Structures.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Recitation 7 Collections. Array List and Linked List Array List and Linked List are implementations of the same interface: List. As a result, they have.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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,
Linked Lists and Generics Written by J.J. Shepherd.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Vectors l Linked Data Structures Dynamic Data Structures.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Chapter 7 Part 1 Edited by JJ Shepherd
Vectors Linked Data Structures
Linked Lists.
Dynamic Data Structures and Generics
CS2011 Introduction to Programming I Arrays (I)
Dynamic Data Structures and Generics
Comp 249 Programming Methodology
Presentation transcript:

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam 2 »Returned next week in recitation »Scores posted ~ Monday/Tuesday »Solution covered today

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 2 Vector Syntax Array: a is a String array a[i] = "Hi, Mom!"; String temp = a[i]; Vector: v is a vector v.setElementAt("Hi, Mom!", i); String temp = (String)v.elementAt(i); Instead of the index in brackets and = for assignment, use vector method setElementAt with two arguments, the value and the index Use vector method elementAt(int index) to retrieve the value of an element Note: the cast to String is required because the base type of vector elements is Object l The idea is the same as for arrays, but the syntax is different l As with arrays, the index must be in the range 0 to (size-of-the-vector – 1) Notice that while arrays are not truly classes, Vectors are!

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 3 A little Detail about setElementAt "The devil's in the details." –an American engineering saying l Vectors put values in successive indexes »addElement is used to put initial values in a vector »new values can be added only at the next higher index You cannot use setElementAt to put a value at just any index »setElementAt can be used to assign the value of an indexed variable only if it has been previously assigned a value with addElement »i.e., you cannot call call setElementAt(data,i) unless element i already exists!

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 4 One More Detail: Size Versus Capacity l Be sure to understand the difference between capacity and size of a vector: »capacity is the declared size of the vector (v.capacity()) –the current maximum number of elements »size is the actual number of elements being used (v.size()) –the number of elements that contain valid values, not garbage –remember that vectors add values only in successive indexes Loops that read vector elements should be limited by the value of size, not capacity, to avoid reading garbage values l This is nice because you don’t have keep track of how many elements actually have valid data, like arrays.

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 5 Programming Tip : Adding to a Vector Can use addElement »adds elements at index positions in order Can also use insertElementAt to add to a vector »specify the position where you want to insert the element: v.insertElementAt(element, index); »index must be less than or equal to size »If index is equal to size, then element will be inserted at the end (the same place where addElement would add it). »If index is greater than size, you will get a run-time error that says ArrayIndexOutOfBoundsException »All elements at position index or higher will have their index increased by 1 »There is also a removeElementAt(index) method

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 6 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 10Java: an Introduction to Computer Science & Programming - Walter Savitch 7 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 10Java: an Introduction to Computer Science & Programming - Walter Savitch 8 Linked Lists "Cheatem" "and" "Howe" head "Duey" One node in the list Data in a node A link in a node Null link signifying the end of the list null l Linked list consists of objects known as nodes l Each node has a place for data and a link to another node l Links are shown as arrows l Each node is an object of a class that has two instance variables: one for the data and one for the link The head of the list is not a node.

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 9 ListNode Class: Instance Variables and Constructor public class ListNode { private String data; private ListNode link; public ListNode(String newData, ListNode linkValue) { data = newData; link = linkValue; } Two parameters for the constructor: l data value for the new node l Link value for the new node "Duey"

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 10 Start at beginning of list Stepping through a List "Cheatem" "and" "Howe" head "Duey" When position is at this last node, position.getLink() is null and the loop will terminate. null ListNode position; position = head; while (position != null) {... position = position.getLink(); } position This reference is position.getLink(). Moves to next node in the list. Excerpt from showList in StringLinkedList

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 11 Accessing the links l Notice the line of code in the previous slide: position = position.getLink(); l We must use this line of code when we are outside of the ListNode class. When we are inside the ListNode class, you can access link directly: position = position.link; Likewise, with the setLink method, outside of the ListNode class we have to use: position.setLink(myNode); While inside the ListNode class we can access the link directly: position.link = myNode; l We will see more examples in future slides.

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 12 Gotcha : Null Pointer Exception l A Null pointer exception occurs when your code tries to access some class variable and the class variable does not name an object. List nodes use null to indicate that a link instance variable contains no reference. NullPointerException is not an exception that has to be caught or declared. »Usually indicates you need to fix your code, not add a catch block. »Just check if objectName == null before you use the object! l In some cases, it is useful to set objects to null – such as in linked lists, where a null link indicates the end of a list.

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 13 Adding a Node l After creating the node, the two statements used to add the node to the list are: newNode.link = current.link; current.link = newNode; l What would happen if these two steps were done in reverse order?

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 14 A Doubly Linked List l A doubly linked list allows the program to move backward as well as forward in the list. l The beginning of the node class for a doubly-linked list would look something like this: private class ListNode { private Object data private ListNode next; private ListNode previous; null Declaring the data reference as class Object allows any kind of data to be stored in the list.

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 15 Other Linked Data Structures l tree data structure »each node leads to multiple other nodes l binary tree »each node leads to at most two other nodes l root—top node of tree »normally keep a reference to root, as for head node of list null root node

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 16 Quiz from Class l Tell how you could use a Vector to implement a Linked list. l html html

Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 17 Summary l Vectors can be thought of as arrays that can grow in length as needed during run time. The base type of all vectors is Object. l Thus, vector elements can be of any class type, but not primitive types. l A linked list is a data structure consisting of objects known as nodes, such that each node can contain data, and each node has a reference to the next node in the list. l You can make a linked list self-contained by making the node class an inner class of the linked list class. l You can use an iterator to step through the elements of a collection.