Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays ICS 111: Introduction to Computer Science I

Similar presentations


Presentation on theme: "Arrays ICS 111: Introduction to Computer Science I"— Presentation transcript:

1 Arrays ICS 111: Introduction to Computer Science I
William Albritton Information and Computer Sciences Department at the University of Hawai‘i at Mānoa "Heavier-than-air flying machines are impossible." Lord Kelvin, president, Royal Society, 1895 11/16/2018 © 2007 William Albritton

2 Terminology Array Array index (subscript) Array element
A list of data of the same type Array index (subscript) A number corresponding to the position of an element in an array Numbered from 0, 1, 2, N-1, where N is the size of the array Array element The value that is stored in an array 11/16/2018 © 2007 William Albritton

3 Array Examples Array of Integers Array of Doubles finalGrades
examScores index address element Array of Doubles finalGrades indices addresses elements 11/16/2018 © 2007 William Albritton

4 Example Code See InitializingArrays.java This program shows how to:
Declare arrays Instantiate arrays Initialize arrays Display arrays 11/16/2018 © 2007 William Albritton

5 Declaring Arrays Syntax for an array declaration Example Java code
DataType [] arrayName; Example Java code Integer [] examScores; Double [] finalGrades; A declaration creates a variable with the address to an array, but not the array itself Since arrays are objects in Java, you must declare an array variable (which will later contain the address to the location of the array object) 11/16/2018 © 2007 William Albritton

6 Declaring Arrays These declarations create a reference variable with a null value (does not point to an object) Integer [] examScores; examScores null Double [] finalGrades; finalGrades null 11/16/2018 © 2007 William Albritton

7 Variables & Objects A variable stores a reference (address) to an object In the below example, name is a variable that will store the address to a String object String name; null In the below example, examScores is a variable that will store the address to an array of Integers Integer [] examScores; null 11/16/2018 © 2007 William Albritton

8 Instantiating Arrays Syntax for array instantiation (creation)
arrayName = new DataType[SIZE]; SIZE is the number of elements in the array Example Java code final Integer SIZE = 6; //constant examScores = new Integer[SIZE]; finalGrades = new Double[SIZE]; This instantiates (creates) an array object & stores its address in a variable The array object will contain no addresses (null) 11/16/2018 © 2007 William Albritton

9 Example Array Instantiation
Array of integers examScores null 1 null 2 null 3 null index null address 5 null Since no addresses have been assigned, the values stored in the array are “null” Array of doubles finalGrades null 1 null 2 null 3 null 4 null 5 null indices addresses 11/16/2018 © 2007 William Albritton

10 Class Exercise 1 Declare an array that is used to store
Student ID numbers The letters of the alphabet Declare and instantiate an array that is used to store All the high temperatures of this week 5 boolean values Draw the “box & arrow” representation of what this looks like in memory In your drawing, include variable labels, addresses (arrows), element values, and index values 11/16/2018 © 2007 William Albritton

11 Initializing Arrays Syntax for initializing arrays Example Java code
arrayName[i] = value; Where i is the index of an element in the array Example Java code examScores[0] = 80; examScores[1] = 77; examScores[2] = 92; examScores[3] = 80; examScores[4] = 63; examScores[5] = 95; 11/16/2018 © 2007 William Albritton

12 Initializer List You can also declare, instantiate, & initialize an array with one statement (initializer list) Syntax DataType [] arrayName = {value0, value1, value2, . . ., valueN-1}; The size of the array is automatically calculated Example Java code Double [] finalGrades = {83.33, 73.56, 91.29, 55.55, 67.02,83.33}; 11/16/2018 © 2007 William Albritton

13 Bug for Initializer List
Can only use initializer list when the array is declared Incorrect Java code Double [] finalGrades; finalGrades = {83.33, 73.56, 91.29, 55.55, 67.02,83.33}; //bug! 11/16/2018 © 2007 William Albritton

14 Array Initialization Array of Integers Array of Doubles finalGrades
examScores index address element Array of Doubles finalGrades indices addresses elements 11/16/2018 © 2007 William Albritton

15 Class Exercise 2 Declare and instantiate and initialize an array that is used to store Vowels Even integers from 2 to 100 Draw the “box & arrow” representation of what this looks like in memory In your drawing, include variable labels, addresses (arrows), element values, and index values 11/16/2018 © 2007 William Albritton

16 Array Size Size of an array is stored in a public variable called length length is not a method! Bug: array.length() Useful in loops for(Integer i=0;i<examScores.length;i++){ System.out.print(examScores[i] + ", "); } 11/16/2018 © 2007 William Albritton

17 Out of Bounds Error What if an array index is greater or less than range of the array? An exception will be thrown & the program will end Example Java code for(Integer i=0;i<=examScores.length; i++){ System.out.print(examScores[i] + ", ");} Program will end and display a message about an ArrayIndexOutOfBoundsException Since arrays start their index numbering at 0, a common bug is to be “off by one” Range for arrays is 0 to SIZE-1 (length-1) 11/16/2018 © 2007 William Albritton

18 for-each Loop A for-each loop loops through all elements of an array from lowest to highest index Will loop array.length times Syntax for(DataType element: arrayName){ //use element in code } Java code example for(Double grade: finalGrades){ System.out.print(grade + ", "); } 11/16/2018 © 2007 William Albritton

19 Class Exercise 3 What is the output of the Java program?
See Exercise3.java 11/16/2018 © 2007 William Albritton


Download ppt "Arrays ICS 111: Introduction to Computer Science I"

Similar presentations


Ads by Google