{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# CH10 - Chapter 10 Dynamic Data Structures Vectors Linked...

This preview shows pages 1–9. Sign up to view the full content.

Chapter 10 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 10 Vectors Linked Data Structures Dynamic Data Structures

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 10 Java: an Introduction to Computer Science & Programming - Walter Savitch Overview This chapter is about data structures that are dynamic : They can grow and shrink while your program is running Vectors are similar to arrays but are more flexible. Linked lists are a dynamic data structure commonly used in many programming languages.
Chapter 10 Java: an Introduction to Computer Science & Programming - Walter Savitch Vectors "Well, I'll eat it," said Alice, "and if it makes me grow larger, I can reach the key; and if it makes me grow smaller, I can creep under the door; so either way I'll get into the garden…" Lewis Carroll, Alice's Adventures in Wonderland VECTORS Think of them as arrays that can get larger or smaller when a program is running.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 10 Java: an Introduction to Computer Science & Programming - Walter Savitch Using Vectors Vectors are not automatically part of Java » they are in the util library » you must import java.util.* Create a vector with an initial capacity of 20 elements: Vector v = new Vector(20);
Chapter 10 Java: an Introduction to Computer Science & Programming - Walter Savitch Initial Capacity and Efficiency: a Classic Engineering Tradeoff Engineering involves making difficult tradeoffs » "There's no such thing as a free lunch." an American saying » Usually, if you gain something you lose something somewhere else Choosing the initial capacity of a vector is an example of a tradeoff » making it too large wastes allocated memory space » making it too small slows execution it takes time to resize vectors dynamically Solution? » optimize one at the expense of the other » or make good compromises choose a size that is not too big and not too small

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 10 Java: an Introduction to Computer Science & Programming - Walter Savitch 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 The idea is the same as for arrays, but the syntax is different As with arrays, the index must be in the range 0 to size-of-the-vector
Chapter 10 Java: an Introduction to Computer Science & Programming - Walter Savitch Vector Methods The vector class includes many useful methods: » constructors » array-like methods, e.g. setElementAt & elementAt » methods to add elements » methods to remove elements » search methods » methods to work with the vector's size and capacity, e.g. to find its size and check if it is empty » a clone method to copy a vector See the text for more information

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}