Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

2 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!

3 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!

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

5 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

6 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”

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

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

9 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"

10 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

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

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

13 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?

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

15 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

16 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 http://web.ics.purdue.edu/~cs180/Spring2005Web/quizzes/q19. html http://web.ics.purdue.edu/~cs180/Spring2005Web/quizzes/q19. html

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


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

Similar presentations


Ads by Google