# Class 14: Arrays and Sequences

Back to Documentation and Testing. On to Algorithm Analysis.

Held Tuesday, February 16

Summary

Contents

Handouts

Notes

• Reminder: you'll get your first exam on Friday. It will be a take-home exam. The questions are likely to be more ``theoretical'' than programming-oriented, but there will be some of both.
• Because some of you still haven't turned in assignment 3, we'll delay discussion until tomorrow. Notes on assignment 3 will be available by the end of the day.
• Are there questions assignment 4 or assignment 5?
• Don't forget, there's still a cool statistics talk on why Bayesian statistics is better than everything, this coming Thursday at 4:30, with refreshments at 4:15.
• As some of you guessed, by giving extra credit for typos, I got many of you to read the lab manual much more closely, hopefully to your benefit.

## Collections

• When writing programs, we often need to refer to groups or collections of things in such a way that we can easily extract an element of the group.
• Surprisingly, there is no common term in the computer science lexicon for ``data structure used to collect things''
• Eventually, you will build your own data types for particular kinds of collections and groups.
• But you will also take advantage of some built-in and library types that Java provides.
• The built-in array (similar to the Vector in Scheme).
• Library classes Hashtable and Vector

### Lists and Vectors

• In Scheme, you saw two primitive kinds of collections: lists and vectors.
• How are they similar?
• How are they different?
• Note that there is significant controversy on the use of the term ``vector'' to name that data type.

## Arrays

• Java provides a structure called the array that makes it easy to create particular kinds of collections.
• Arrays are fixed-sized collections of elements that you can index by number.
• When you create an array, you must specify its length in advance.
• Array indices range from 0 to length-1.
• Arrays are somewhat homogeneous: all the elements are of the same type (or a subclass of the same type).
• You can declare arrays of a particular size with
type[] name = new type[size];

• For example, to create a collection of five numbers named stuff, you would write
double[] stuff = new double(5);

• You can also fill in arrays when you create them. To do this, you would write
type[] name = { val1, val2, ..., valn };

• For example, to create an array with the values 3, 2, 6, and 5, I would write
int[] stuff = {3, 2, 6, 5};

• You can refer to a particular element of an array with name[index]
• For example, I could element 3 of the stuff with stuff[3]
• You can also use variables (or even expressions) as indices. For example, if i held the value 3, then we could get element 3 of the stuff with stuff[i]
• You can always determine the number of elements in an array with name.length
• Once we have values in arrays, it's "easy" to do general computations with them. Here is a method that sums the values in an array.
public double sum(double[] stuff) {
double total = 0;	// The total of the numbers in stuff
int i;		// A counter variable for stepping through
// the array
// Step through the array, adding each subsequent value
for(i = 0; i < stuff.length; i = i + 1) {
total = total + stuff[i];
}
// Return the total value
} // sum

History

• Created Monday, January 11, 1999.
• Added short summary on Friday, January 22, 1999.
• Filled in the details on Tuesday, February 16, 1999. These details were based, in part, on outline 13 from CSC152 98S.

Back to Documentation and Testing. On to Algorithm Analysis.

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.