Download presentation
Presentation is loading. Please wait.
Published byViolet Dennis Modified over 9 years ago
1
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is used to store data elements of the same data type. An array can be thought of as Post Office boxes. Each box is indexed with a specific address. An array can be thought of as Post Office boxes. Each box is indexed with a specific address. Components are accessed using their relative positions in the array Components are accessed using their relative positions in the array A single component is called an indice. A single component is called an indice.
2
Arrays cont…. An array is an object. Because of this, the array name is a reference variable. 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. Therefore, in order to start using an array, it must be instantiated just like any other object.
3
One-dimensional Arrays 8.55 1.65 7.53 5.45 1.24 4.43 9.37
4
Defining One-Dimensional Arrays To define an array you must specify three things: To define an array you must specify three things: The data type of the array elements The data type of the array elements The name of the array The name of the array The size of the array The size of the array Array Format Array Format dataType[] arrayName = new dataType[size]; dataType[] arrayName = new dataType[size];
5
Array Subscripts Subscripts are used to refer to a specific array element. 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 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. The subscript range of an array is listed in brackets in the array declaration. Array subscripts may only be scalar data types. Array subscripts may only be scalar data types. An individual array element must have a subscript which is within the allowed subscript values. An individual array element must have a subscript which is within the allowed subscript values.
6
Giving an Array Element a Value through Direct Assignment It is possible to give a direct assignment to an array element. It is possible to give a direct assignment to an array element. Format: Format: int[] x = new int[5]; int[] x = new int[5]; x[1]=5; x[1]=5;
7
Reading and Writing to Arrays The easiest way to read or write to an array is by using loops. 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. 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. It is not necessary to know array size at compile time. arrayName.length returns the number of components in array arrayName.length returns the number of components in 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 (x= 0; x<3; x++) System.out.print(examp[x] + “,”); Array examp Output: 1,2,3, 1 2 3examp[0]examp[1] examp[2]
9
Specifying an Array Size During Program Execution int arraySize; arraySize = reader.readInt("Enter the size of the array: "); int[] list = new int[arraySize];
10
Array Index Out of Bounds Array in bounds if: Array in bounds if: 0 <= index <= arraySize – 1 If index arraySize: If index arraySize: ArrayIndexOutOfBoundsException exception is thrown Base address: memory location of first component in array Base address: memory location of first component in array
11
Two-Dimensional Array Example int [][] example = new int[3][2]; for (int x=0; x<3; x++) for (int y=0; y<2; y++) example[x][y]=x*y ; example[][][0][1] [0]00 [1]01 [2]02
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.