vectors - Computer Science 211 Data Structures Mount...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science 211 Data Structures Mount Holyoke College Fall 2009 Topic Notes: Vectors Arrays are a very common method to store a collection of similar items. Arrays work very well for a lot of situations, but they come with some very important restrictions. their size is specified on construction, and cannot be changed without constructing a new array and copying over the contents all array indices must be managed explicitly if you want to insert an item at the start of or in the middle of an array, you need to move one or more items out of the way to make room if you remove an item from the start or the middle of an array and you don’t want to leave a “hole” in the middle, one or more items needs to be moved around to fill in the hole This idea of a dynamically resizeable (or, extensible ) array leads naturally to the idea of a vector . The built-in Java class java.util.Vector allows its user to build something like an array, but it can change size dynamically. We can add new elements or delete elements anywhere in the vector. What kinds of operations would we like to have on something that behaves like a resizeable array? We need the functionality of a regular array: construction add an item to the end insert an item in the middle retrieve value of an element remove an item Most of the operators of a vector will assume that the elements are “packed” – that is: if we add an element, it will be added to the end by default if we add an element in the middle, all elements with higher subscripts are moved up to make room
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 211 Data Structures Fall 2009 if we remove an element, all elements with higher subscripts are shifted down to fill in the space So we already have some extra functionality that a regular array doesn’t have. Since the vector needs to be able to hold anything, its elements are of type Object (until we look at generics shortly), hence our initial implementation will use casts when items are retrieved. Here are the key methods we will consider in the implementation of Vector in structure package (which mimics the one in java.util ). public class Vector { // post: constructs a vector with capacity for 10 elements public Vector() // post: adds new element to end of possibly extended vector public void add(Object obj) // post: returns true iff Vector contains the value public boolean contains(Object elem)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 02/15/2010.

Page1 / 6

vectors - Computer Science 211 Data Structures Mount...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online