You are on page 1of 14

Arrays

54
Arrays
• Array is used to store a collection of data of
the same type.
• Instead of declaring individual variables,
such as number0, number1, ..., and number99.
• You declare one array variable such as
numbers and use numbers[0], numbers[1], and
..., numbers[99] to represent individual
variables.

55
Arrays

double[] myList = new double[10];

myList reference
myList[0] 5.6
myList[1] 4.5
Array reference myList[2] 3.3
variable
myList[3] 13.2

myList[4] 4
Array element at
myList[5] 34.33 Element value
index 5
myList[6] 34

myList[7] 45.45

myList[8] 99.993

myList[9] 11123

56
Arrays
• Declaring Array
– dataType [] arrayRefVar;
• Example:
– int[] arrayName;
• Creating Arrays
− arrayRefVar = new dataType[arraySize];
• Example:
− arrayName = new int[5];

57
Arrays
• When we need to access the array we can
do so directly as in:
for (int i=0; i<5; i++)
{
System.out.println( arrayName[i] );
} // end for

58
Example
double[] myList = new double[10];
for (int i = 1; i < myList.length; i++)
{
myList[i]=i;
}

for (int i = 1; i < myList.length; i++)


{
System.out.println(i);
}

59
Array Initializers
• Declaring, creating, initializing in one step:
double [] myList = {1.9, 2.9, 3.4, 3.5};

• This shorthand syntax must be in one


statement and it’s equivalent to the following
statements:
double [] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
60
Passing Arrays to Methods
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}

Invoke the method

int[] list = {3, 1, 2, 6, 4, 2};


printArray(list);

Invoke the method


printArray(new int[]{3, 1, 2, 6, 4, 2});

Anonymous array

61
Returning an Array from a
Method
public static int[] reverse(int[] list)
{
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1;


i < list.length; i++, j--) {
result[j] = list[i];
}

return result;
}

int[] list1 = {1, 2, 3, 4, 5, 6};


int[] list2 = reverse(list1);

62
Two-dimensional Arrays
// Declare array ref var
dataType[][] refVar;

// Create array and assign its reference to


variable
refVar = new dataType[10][10];

// Combine declaration and creation in one


statement
dataType[][] refVar = new dataType[10][10];

// Alternative syntax
dataType refVar[][] = new dataType[10][10];

63
Declaring Variables of Two-
dimensional Arrays and Creating
Two-dimensional Arrays
int[][] matrix = new int[10][10];
or
int matrix[][] = new int[10][10];
matrix[0][0] = 3;

for (int i = 0; i < matrix.length; i++)


for (int j = 0; j < matrix[i].length; j++)
matrix[i][j] = (int)(Math.random() * 1000);

64
Declaring, Creating, and
Initializing Using Shorthand
Notations
• You can also use an array initializer to declare,
create and initialize a two-dimensional array.
For example,
int[][] array
int[][] array = new int[4][3];
= {
array[0][0] = 1; array[0][1] = 2;
{1, 2, 3}, array[0][2] = 3;
{4, 5, 6},
Same as
array[1][0] = 4; array[1][1] = 5;
{7, 8, 9}, array[1][2] = 6;
array[2][0] = 7; array[2][1] = 8;
{10, 11,
array[2][2] = 9;
12}
array[3][0] = 10; array[3][1] = 11;
}; array[3][2] = 12;

65
Lengths of Two-dimensional
Arrays
int[][] x = new int[3][4];

x
x[0][0] x[0][1] x[0][2] x[0][3] x[0].length is 4
x[0]
x[1] x[1][0] x[1][1] x[1][2] x[1][3] x[1].length is 4

x[2]
x[2][0] x[2][1] x[2][2] x[2][3] x[2].length is 4
x.length is 3

66
Lengths of Two-dimensional
Arrays, cont.
int[][] array = { array.length
{1, 2, 3}, array[0].length
{4, 5, 6}, array[1].length
{7, 8, 9}, array[2].length
{10, 11, 12} array[3].length
};

array[4].length
ArrayIndexOutOfBoundsException

67

You might also like