ChapterSixPartI - IT108 Arrays George Mason University...

Info iconThis preview shows pages 1–11. 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

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: IT108 Arrays George Mason University Introducing Arrays Array is a data structure that represents a collection of the same types of data. 5.6 4.5 3.3 13.2 4 34.33 34 45.45 99.993 11123 double myList = new double[10]; myList reference myList[0] myList[1] myList[2] myList[3] myList[4] myList[5] myList[6] myList[7] myList[8] myList[9] Element value Array reference variable Array element at index 5 Declaring Array Variables datatype arrayRefVar; Example: double myList; datatype arrayRefVar; // This style is allowed, but not preferred Example: double myList; Creating Arrays arrayRefVar = new datatype[arraySize]; Example: myList = new double[10]; myList[0] references the first element in the array. myList[9] references the last element in the array. Declaring and Creating in One Step datatype arrayRefVar = new datatype[arraySize]; double myList = new double[10]; datatype arrayRefVar = new datatype[arraySize]; double myList = new double[10]; The Length of an Array Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayRefVar.length For example, myList.length is 10 Default Values When an array is created, its elements are assigned the default value of for the numeric primitive data types, '\u0000' for char types, and false for boolean types. Indexed Variables The array elements are accessed through the index starting from 0 to arrayRefVar.length-1. Each element in the array is represented using the following syntax, known as an indexed variable : arrayRefVar[index]; Using Indexed Variables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in myList[0] and myList[1] to myList[2] . myList[2] = myList[0] + myList[1]; Array Initializers Declaring, creating, initializing in one step: double myList = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement. Declaring, creating, initializing Using the Shorthand...
View Full Document

This note was uploaded on 01/26/2012 for the course IT 108, 103, taught by Professor Bruno during the Spring '11 term at George Mason.

Page1 / 31

ChapterSixPartI - IT108 Arrays George Mason University...

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

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