Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.

Similar presentations


Presentation on theme: "Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index."— Presentation transcript:

1 Chapter 9 Arrays

2 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

3 Chapter Objectives Discover how to pass an array as a parameter to a method Discover how to manipulate data in a two- dimensional array Learn about multidimensional arrays

4 Array Definition: structured 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

5 One-Dimensional Arrays Syntax to instantiate an array: –dataType[ ] arrayName; arrayName = new dataType[intExp] –dataType[ ] arrayName = new dataType[intExp] –dataType[ ] arrayName1, arrayName2; Syntax to access an array component: –arrayName[indexExp] intExp = number of components in array >= 0 0 <= indexExp <= intExp

6 Array num: int[] num = new int[5];

7 Array list

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

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

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

11 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

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

13 Code to Read Data into Array for(index = 0; index < sale.length; index++) sale[index] = Integer.parseInt(keyboard.readLine());

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

15 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;

16 Determining Largest Element in Array maxIndex = 0; for(index = 1; index < sale.length; index++) if(sale[maxIndex] < sale[index]) maxIndex = index; largestSale = sale[maxIndex];

17 Determining Largest Element in Array

18 Array Index Out of Bounds Array in bounds if: 0 <= index <= arraySize – 1 If index arraySize: ArrayIndexOutOfBoundsException exception is thrown Base address: memory location of first component in array

19 The Assignment Operator, the Relational Operator, and Arrays

20

21 Methods for Array Processing

22

23

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

25 Arrays of Objects Can use arrays to manipulate objects Example: create array named array1 with N objects of type T T [ ] array1 = new T[N] Can instantiate array1 as follows: for(int j=0; j <array1.length; j++) array1[j] = new T();

26 Arrays of Objects: Clock[] arrivalTimeEmp = new Clock [100];

27 Instantiating Array Objects

28 Two-Dimensional Arrays Data is sometimes in table form (difficult to represent using one-dimensional array) To declare/instantiate two-dimensional array: dataType[ ][ ] arrayName = new dataType[intExp1][intExp2]; To access a component of a 2-dimensional array: arrayName[indexExp1][indexExp2]; intExp1, intExp2 >= 0 indexExp1 = row position indexExp2 = column position

29 Two-Dimensional Arrays Can specify different number of columns for each row (ragged arrays) 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

30 double[][]sales = new double[10][5]; Two-Dimensional Arrays

31 Accessing Two-Dimensional Array Components

32 Two-Dimensional Arrays: Special Cases

33 Multidimensional Arrays Can define three-dimensional arrays or n- dimensional array (n can be any number) Syntax to declare and instantiate array: d ataType[ ][ ]…[ ] arrayName = new dataType[intExp1][intExp2]…[intExpn]; Syntax to access component: arrayName[indexExp1][indexExp2]…[indexExpn] intExp1, intExp2,..., intExpn = positive integers indexExp1,indexExp2,..., indexExpn = non-negative integers

34 Loops to Process Multidimensional Arrays double[][][] carDealers = new double [10][5][7]; for(i = 0; i < 10; i++) for(j = 0; j < 5; j++) for(k = 0; k < 7; k++) carDealers[i][j][k] = 10.00;

35 Programming Example: Text Processing Program: reads given text; outputs the text as is; prints number of lines and number of times each letter appears in text Input: file containing text to be processed Output: file containing text, number of lines, number of times letter appears in text

36 Programming Example Solution: Text Processing An array of 26 representing the letters in the alphabet Three methods: –copyText –characterCount –writeTotal Value in appropriate index incremented using methods and depending on character read from text

37 Chapter Summary Arrays –Definition –Uses Different Arrays –One-dimensional –Two-dimensional –Multidimensional (n-dimensional) –Arrays of objects –Parallel arrays

38 Chapter Summary Declaring arrays Instantiating arrays Processing arrays –Entire array –Row processing –Column processing Common operations and methods performed on arrays Manipulating data in arrays


Download ppt "Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index."

Similar presentations


Ads by Google