11

Click here to load reader

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

Embed Size (px)

DESCRIPTION

One-dimensional Arrays

Citation preview

Page 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

ArraysArraysAn array is an indexed data structure An array is an indexed data structure

which is used to store data elements of the which is used to store data elements of the same data type.same data type.

An array can be thought of as Post Office An array can be thought of as Post Office boxes. Each box is indexed with a specific boxes. Each box is indexed with a specific address.address.

Components are accessed using their Components are accessed using their relative positions in the arrayrelative positions in the array

A single component is called an indice.A single component is called an indice.

Page 2: 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

Arrays cont….Arrays cont….An array is an object. Because of this, the An array is an object. Because of this, the

array name is a reference variable.array name is a reference variable.Therefore, in order to start using an array, Therefore, in order to start using an array,

it must be instantiated just like any other it must be instantiated just like any other

object.object.

Page 3: 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

One-dimensional ArraysOne-dimensional Arrays

8.551.657.535.451.244.439.37

Page 4: 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

Defining One-Dimensional ArraysDefining One-Dimensional Arrays

To define an array you must specify three To define an array you must specify three things:things:The data type of the array elementsThe data type of the array elementsThe name of the arrayThe name of the arrayThe size of the arrayThe size of the array

Array FormatArray Format dataType[] arrayName = new dataType[size];dataType[] arrayName = new dataType[size];

Page 5: 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

Array SubscriptsArray Subscripts Subscripts are used to refer to a specific array Subscripts are used to refer to a specific array

element.element. The first element is assigned subscript of 0; the The first element is assigned subscript of 0; the

next, a subscript of 1, and so onnext, a subscript of 1, and so on The subscript range of an array is listed in The subscript range of an array is listed in

brackets in the array declaration.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 An individual array element must have a

subscript which is within the allowed subscript subscript which is within the allowed subscript values.values.

Page 6: 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

Giving an Array Element a Value through Giving an Array Element a Value through Direct AssignmentDirect Assignment

It is possible to give a direct assignment to It is possible to give a direct assignment to an array element.an array element.

Format:Format: int[] x = new int[5];int[] x = new int[5];

x[1]=5;x[1]=5;

Page 7: 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

Reading and Writing to ArraysReading and Writing to ArraysThe easiest way to read or write to an The easiest way to read or write to an

array is by using loops.array is by using loops.Often times you will want to use the loop Often times you will want to use the loop

control variable as the array subscript.control variable as the array subscript. It is not necessary to know array size at It is not necessary to know array size at

compile time.compile time.arrayName.length returns the number of arrayName.length returns the number of

components in arraycomponents in array

Page 8: 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

Example of a 1-Dimensional Example of a 1-Dimensional ArrayArray

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,

112233

examp[0]examp[0]

examp[1]examp[1]

examp[2]examp[2]

Page 9: 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

Specifying an Array Size During Specifying an Array Size During Program ExecutionProgram Execution

int arraySize; arraySize = reader.readInt("Enter the size of the array: ");

int[] list = new int[arraySize];

Page 10: 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

Array Index Out of Bounds Array Index Out of Bounds Array in bounds if:Array in bounds if:

0 <= index <= arraySize 0 <= index <= arraySize –– 1 1 If index < 0 or index > arraySize:If index < 0 or index > arraySize:

ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException exception is thrownexception is thrown

Base address: memory location of first Base address: memory location of first component in arraycomponent in array

Page 11: 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

Two-Dimensional Array ExampleTwo-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[][]example[][] [0][0] [1][1][0][0] 00 00[1][1] 00 11[2][2] 00 22