lect6arrays_4up

lect6arrays_4up - CMPT 126: Lecture 6 Arrays Tamara Smyth,...

Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPT 126: Lecture 6 Arrays Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University September 25, 2007 1 Array Elements An array is a construct used to group and organize data. By using an array, we dont have to declare separate variables for large amounts of data. An array is a list of values of the same type, each one stored at a specific numbered position called an index . array elements 5 4 3 2 1 indeces 88 78 32 42 67 4 Figure 1: An array. An array of size N is indexed from 0 to N- 1 . CMPT 126: Arrays, Lecture 6 2 Declaring and Using Arrays In Java, an array is an object that must be instantiated. To create an array, the reference to the array must be declared: int height = new int[11]; where the variable is height , which is declared to be an array of eleven integers. The instantiation of height , using the new operator, reserves the memory space to store 11 integers. The size of an array cannot be changed. To access a value in an array, we use the name of the array followed by the index in square brackets: height[2] = 45; CMPT 126: Arrays, Lecture 6 3 Initializer list The array may also be initialized at declaration using an initializer list , in which case the new operator is not used: int height = {2, 34, 45, 22, 235}; An initializer list can only be used when an array is first declared. The type of each value in the initializer list must match the array element type. CMPT 126: Arrays, Lecture 6 4 PrintArray.java It is often convenient to use for loops when handling arrays because the number of positions in the array is constant. public class PrintArray { public static void main(String args) { final int LIMIT = 15, MULTIPLE = 10; int list = new int[LIMIT]; // initialize the array values for (int i = 0; i< LIMIT; i++) list[i] = i * MULTIPLE; list[5] = 999; //change one value // Print the array values for (int value : list) System.out.print(value + " "); } } Every Java array is an iterator so the version of for that extracts each value in the specified iterator may be used. CMPT 126: Arrays, Lecture 6 5 Bounds Checking Bounds checking ensures that an index used to refer to an array element is in range. The index operator performs automatic bounds checking , though we will still want to perform our own bounds checking. A reference to an array must be greater than or equal to zero , and less than the size of the array . The length of the array may be determined using its length constant....
View Full Document

Page1 / 7

lect6arrays_4up - CMPT 126: Lecture 6 Arrays Tamara Smyth,...

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