# Outline of Class 13: The Vector

Held: Monday, February 9, 1998

• At noon today, Randy Smith of Torrent Systems, Inc., a "Parallel Software Infrastructure Company", will give a talk entitled The Programming They Don't Teach You In College.
• Barry McKeown and Charlotte Abe of Towers Perrin will speak on "What Does An Actuary Actually Do" Thursday, February 12, 4:30 p.m. in Science 2413. Refreshments at 4:15.
• Our Convocation speaker, Prof. Doris Schattschneider, will be having lunch with students at Grinnell House on Wednesday, February 25. Please send a note to Prof. Herman if you would like to join her for lunch.
• Today we'll be returning to the topic of data structures, the primary focus of this course.

## Vectors

• One of the simplest data types is the vector.
• Different people define vectors somewhat differently.
• Many people use the term array to refer to structures like Vectors.
• Bailey's vector is but one version of what a vector can be; it is designed to match the built-in Java Vector.
• Java also provides an array class.
• The primary intent of vectors and arrays is that you have a collection of objects of similar types (sometimes the same type) that you can reference by number (using numbers in some reasonable range).
• What questions might you ask in changing that vague definition to a more concrete list of methods?
• What constructors should we support?
• Create new vectors?
• Create new vectors of specified size?
• Create new vectors of specified size with the same element in each position?
• Create new vectors of specified size with different elements in each position?
• Do we differentiate between the number of elements in a vector, the number of valid indices, and the number of elements the vector can hold?
• Can vectors grow in size? Can they shrink?
• Can we change an assigned element?
• What happens if we can delete elements from the vector?
• Do the remaining elements change position?
• Do we simply get a hole in the vector?
• Do we get a default value at that position?
• ...
• Do we have a default/required lower-end of the range of indices?
• What happens if an illegal request is made?
• And what are potentially illegal requests?
• What syntax should we use for making the requests?
• You may be surprised by some of the answers the designers of Java chose.

## Arrays

• Java has a built-in (but not primitive) type called the array that uses a different syntax than other objects.
• Like vectors, arrays provide storage for a fixed number of things.
• The things may be objects.
• The things may be primitive types.
• To declare an array of things, you write
`thing[] name;`
• For example, to declare an array of integers called `numbers`, you would write
`int[] numbers;`
• You create new arrays with the `new` command, followed by the type, followed by the number of things you want. If I wanted `numthings` numbers in the previous array, I could write
`numbers = new int[numthings];`
• I could also combine the two
`int[] numbers = new int[numthings];`
• You can create an initialize the array to a collection of values by surrounding the values with curly braces.
• For example,
`int[] fibs = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 }`
• Java uses zero-based arrays. The initial thing in an array is element 0. If there are `n` things in an array, then the last thing is number `n-1`.
• You can refer to a particular element of the array as
`name[number]`
• For example, I could get the tenth element of `numbers` with
`numbers[10]`
• You can determine the length of an array with `name.length`

## How Do Vectors and Arrays Differ?

• Java provides both built-in arrays and a `java.util.Vector` class.
• How are they similar?
• They both provide collections of elements that you can index by number.
• Both work like objects. You create reference variables and then make the variables refer to them.
• How are they different? In a number of ways:
• Arrays are built in to Java; Vectors are part of a library.
• Arrays use a special syntax; Vectors use a normal syntax.
• Arrays are fixed size; Vectors can grow and shrink.
• You can store primitive types in arrays; you cannot store primitive types in Vectors.
• You can have gaps in arrays; you cannot have gaps in Vectors.
• You can initialize arrays when defining them; it is much more difficult to initialize Vectors.

On to Vectors, Continued
Back to Javadoc and Java Wrapup
Outlines: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
Current position in syllabus

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.