Chapter 9 Arrays Chapter Objectives Learn about arrays

Chapter 9 Arrays

Chapter Objectives • Learn about arrays • Explore how to declare and manipulate data into arrays • Understand the meaning of “array index out of bounds” • Become familiar with the restrictions on array processing

Chapter Objectives • Discover how to pass an array as a parameter to a method • Discover how to manipulate data in a twodimensional array • Learn about multidimensional arrays

Array • Definition: structured (or aggregate) data type with a fixed number of components • Every component is of the same type • Components are accessed using their relative positions in the array
![One-Dimensional Arrays • [ ] is the array subscripting operator • Syntax to instantiate One-Dimensional Arrays • [ ] is the array subscripting operator • Syntax to instantiate](http://slidetodoc.com/presentation_image_h2/d8ae87d26bb40bc23d270bebd1b5a906/image-5.jpg)
One-Dimensional Arrays • [ ] is the array subscripting operator • Syntax to instantiate an array: – data. Type[ ] array. Name; array. Name = new data. Type[int. Exp] – data. Type[ ] array. Name 1, array. Name 2; – data. Type array. Name 1[ ], variable. Name 1; • Syntax to access an array component: – array. Name[index. Exp] • int. Exp = number of components in array >= 0 • 0 <= index. Exp < int. Exp
![Initializing Arrays • int[] list = {10, 20, 30, 40, 50} • Creates the Initializing Arrays • int[] list = {10, 20, 30, 40, 50} • Creates the](http://slidetodoc.com/presentation_image_h2/d8ae87d26bb40bc23d270bebd1b5a906/image-6.jpg)
Initializing Arrays • int[] list = {10, 20, 30, 40, 50} • Creates the integer array list with length 5 and initializes the components to the given values
![Array num: int[] num = new int[5]; Array num: int[] num = new int[5];](http://slidetodoc.com/presentation_image_h2/d8ae87d26bb40bc23d270bebd1b5a906/image-7.jpg)
Array num: int[] num = new int[5];

Array list

Arrays • • • Not necessary to know array size at compile time array. Name. length returns the number of components in array Loops used to step through elements in array and perform operations

Arrays • Some operations on arrays: – – Initialize Input data Output stored data Find largest/smallest/sum/average of elements

How To Specify Array Size During Program Execution int array. Size; System. out. print("Enter the size of the array: "); array. Size = Integer. parse. Int(keyboard. read. Line()); System. out. println(); //Line 1 //Line 2 //Line 3 //Line 4 int[] list = new int[array. Size]; //Line 5

Instance Variable length • Contains size of array • public member • Can be directly accessed in program using array name and dot operator • Example – If: int[] list = {10, 20, 30, 40, 50, 60}; – Then: list. length is 6

Code to Initialize Array to Specific Value (10. 00) for(index = 0; index < sale. length; index++) sale[index] = 10. 00;

Code to Read Data into Array for(index = 0; index < sale. length; index++) sale[index] = Integer. parse. Int(keyboard. read. Line());

Code to Print Array for(index = 0; index < sale. length; index++) System. out. print(sale[index] + " ");

Code to Find Sum and Average of Array sum = 0; for(index = 0; index < sale. length; index++) sum = sum + sale[index]; if(sale. length != 0) average = sum / sale. length; else average = 0. 0;

Determining Largest Element in Array max. Index = 0; for(index = 1; index < sale. length; index++) if(sale[max. Index] < sale[index]) max. Index = index; largest. Sale = sale[max. Index];

Determining Largest Element in Array

Array Index Out of Bounds • Array in bounds if: 0 <= index <= array. Size – 1 • If index < 0 or index >= array. Size: Array. Index. Out. Of. Bounds. Exception exception is thrown • Base address: memory location of first component in array

The Assignment Operator, the Relational Operator, and Arrays

The Assignment Operator, the Relational Operator, and Arrays

Methods for Array Processing

Methods for Array Processing

Methods for Array Processing

Parallel Arrays • Arrays are parallel if corresponding components hold related information

Arrays of Objects • Can use arrays to manipulate objects • Example: create array named array 1 with N objects of type T T [ ] array 1 = new T[N] • Can instantiate elements of array 1 as follows: for(int j=0; j <array 1. length; j++) array 1[j] = new T();
![Arrays of Objects: Clock[] arrival. Time. Emp = new Clock [100]; Arrays of Objects: Clock[] arrival. Time. Emp = new Clock [100];](http://slidetodoc.com/presentation_image_h2/d8ae87d26bb40bc23d270bebd1b5a906/image-27.jpg)
Arrays of Objects: Clock[] arrival. Time. Emp = new Clock [100];

Instantiating Array Objects

Two-Dimensional Arrays • Data is sometimes in table form (difficult to represent using one-dimensional array). Think in terms of an 1 -D array of 1 -D arrays. • To declare/instantiate two-dimensional array: data. Type[ ][ ] array. Name = new data. Type[int. Exp 1][int. Exp 2]; • To access a component of a 2 -dimensional array: array. Name[index. Exp 1][index. Exp 2]; • int. Exp 1, int. Exp 2 >= 0 • index. Exp 1 = row position • index. Exp 2 = column position

Two-Dimensional Arrays • Can specify different number of columns for each row (ragged arrays) int two. D[ ][ ] = new int[3][ ]; two. D[0] = new int[5]; two. D[1] = new int[3]; two. D[2] = new int[7]; • two. D. length gives the number of rows • two. D[i]. length gives the number of elements in row I (number of columns)
![Two-Dimensional Arrays • Initialization at Declaration int[ ] mine = { { 2, 3 Two-Dimensional Arrays • Initialization at Declaration int[ ] mine = { { 2, 3](http://slidetodoc.com/presentation_image_h2/d8ae87d26bb40bc23d270bebd1b5a906/image-31.jpg)
Two-Dimensional Arrays • Initialization at Declaration int[ ] mine = { { 2, 3 }, {15, 25, 13}, {20, 4, 7, 8} }; • Three ways to process 2 -D arrays – Entire array – Particular row of array (row processing) – Particular column of array (column processing) • Processing algorithms similar to processing algorithms of one-dimensional arrays

Two-Dimensional Arrays: Special Cases
![Two-Dimensional Arrays double[][]sales = new double[10][5]; Two-Dimensional Arrays double[][]sales = new double[10][5];](http://slidetodoc.com/presentation_image_h2/d8ae87d26bb40bc23d270bebd1b5a906/image-33.jpg)
Two-Dimensional Arrays double[][]sales = new double[10][5];

Accessing Two-Dimensional Array Components

Multidimensional Arrays • Can define three-dimensional arrays or ndimensional array (n can be any number) • Syntax to declare and instantiate array: data. Type[ ][ ]…[ ] array. Name = new data. Type[int. Exp 1][int. Exp 2]…[int. Expn]; • Syntax to access component: array. Name[index. Exp 1][index. Exp 2]…[index. Expn] • int. Exp 1, int. Exp 2, . . . , int. Expn = positive integers • index. Exp 1, index. Exp 2, . . . , index. Expn = non-negative integers
![Loops to Process Multidimensional Arrays double[][][] car. Dealers = new double [10][5][7]; for(i = Loops to Process Multidimensional Arrays double[][][] car. Dealers = new double [10][5][7]; for(i =](http://slidetodoc.com/presentation_image_h2/d8ae87d26bb40bc23d270bebd1b5a906/image-36.jpg)
Loops to Process Multidimensional Arrays double[][][] car. Dealers = new double [10][5][7]; for(i = 0; i < 10; i++) for(j = 0; j < 5; j++) for(k = 0; k < 7; k++) car. Dealers[i][j][k] = 10. 00;
- Slides: 36