11-ArraysAndArrayLists

11-ArraysAndArrayLists - The Art and Science of An...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Jav a Arrays and ArrayLists C H A P T E R 1 1 Little boxes, on a hillside, little boxes made of ticky-tacky Little boxes, little boxes, little boxes, all the same There’s a green one and a pink one and a blue one and a yellow one And they're all made out of ticky-tacky and they all look just the same 11.1 Introduction to arrays 11.2 Internal representation of arrays 11.3 Passing arrays as parameters 11.4 Using arrays for tabulation 11.5 Initialization of arrays —Malvina Reynolds, “Little Boxes,” 1962 11.6 Multidimensional arrays 11.7 Image processing 11.8 The ArrayList class Introduction to Arrays • An array is a collection of individual data values with two distinguishing characteristics: • The individual values in an array are called elements . The type of those elements (which must be the same because arrays are homogeneous) is called the element type . The number of elements is called the length of the array. • Each element is identified by its position number in the array, which is called its index . In Java, index numbers always begin with 0 and therefore extends up to one less than the length of the array. An array is ordered. You must be able to count off the values: here is the first, here is the second, and so on. 1. An array is homogeneous. Every value in the array must have the same type. 2. Declaring an Array Variable • As with any other variable, array variables must be declared before you use them. In Java, the most common syntax for declaring an array variable looks like this: type name = new type [ n ]; where type is the element type, name is the array name, and n is an integer expression indicating the number of elements. • This declaration syntax combines two operations. The part of the line to the left of the equal sign declares the variable; the part to the right creates an array value with the specified number of elements and then assigns it to the array variable. • Even though the two operations are distinct, it will help you avoid errors if you make a habit of initializing your arrays when you declare them. An Example of Array Declaration • The following declaration creates an array called intArray consisting of 10 values of type int : int intArray = new int[10]; • This easiest way to visualize arrays is to think of them as a linear collection of boxes, each of which is marked with its index number. You might therefore diagram the intArray variable by drawing something like this: 1 2 3 4 5 6 7 8 9 intArray • Java automatically initializes each element of a newly created array to its default value , which is zero for numeric types, false for values of type boolean , and null for objects. Array Selection • Given an array such as the intArray variable at the bottom of this slide, you can get the value of any element by writing the index of that element in brackets after the array name....
View Full Document

This note was uploaded on 10/25/2010 for the course CS 106A at Stanford.

Page1 / 55

11-ArraysAndArrayLists - The Art and Science of An...

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

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