Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 9 Arrays. Miscellaneous About Arrays An array is an object. Because of this, the array name is a reference variable Therefore, in order to start.

Similar presentations


Presentation on theme: "Lesson 9 Arrays. Miscellaneous About Arrays An array is an object. Because of this, the array name is a reference variable Therefore, in order to start."— Presentation transcript:

1 Lesson 9 Arrays

2 Miscellaneous About Arrays An array is an object. Because of this, the array name is a reference variable Therefore, in order to start using an array, it must be instantiated just like any other object. For this chapter, we will discuss –one-dimensional arrays –two-dimensional arrays

3 8.55 1.65 7.53 5.45 1.24 One-dimensional array 8.551.657.535.451.24 3.461.898.762.347.89 9.874.567.321.345.78 Two-dimensional array

4 Defining One-Dimensional Arrays To define an array you must specify three things: –the data type of the array elements –the name of the array –the size of the array int list[]; int[] list;

5 Array Subscripts Subscripts are used to refer to a specific array element. The first element is assigned subscript of 0; the next, a subscript of 1, and so on The subscript range of an array is listed in brackets in the array declaration And individual array element must have a subscript which is within the allowed subscript values.

6 Giving an Array and Element a Value Through Direct Assignment int[] x = new int[5]; x[1] = 5;

7 Reading and Writing to Arrays The easiest way to read or write to an array is by using loops. Often times you will want to use the loop control variable as the array subscript. It is not necessary to know array size at compile time. arrayName.length returns the number of components in an array

8 Example of a 1-Dimensional Array int[] examp = new int[3]; for (int x = 0; x < examp.length; x ++) examp[x] = x + 1; for (int x = 0; x<3; x++) System.out.print(examp[x] + “,”);

9 Specifying an Array Size During Program Execution int arraySize; System.out.print(“Enter the size of the array: “); arraySize = Integer.parseInt(keyboard.readLine()); int[] list = new int[arraySize]

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

11 Code to read data into an array for(index = 0; index <sale.length; index++) sale[index] = Integer.parseInt(keyboard.readLine());

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

13 Code to Find Sum and Average of an 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;

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

15 Array Index Out of Bounds Array in bounds if: 0 <= index <= arraySize -1 if index arraySize: ArrayIndexOutOfBoundsException

16 Two – Dimensional Arrays

17 Bottoms Up


Download ppt "Lesson 9 Arrays. Miscellaneous About Arrays An array is an object. Because of this, the array name is a reference variable Therefore, in order to start."

Similar presentations


Ads by Google