Professional Documents
Culture Documents
Lecture # 3
Outline
Arrays
One-Dimensional Arrays
Multidimensional Arrays
Alternative Array Declaration Syntax
Finding Length of an Array
2
Arrays
An array is a group of like-typed variables
that are referred to by a common name.
Arrays of any data type can be created and
may have one or more dimensions.
A specific element in an array is accessed by its
index.
3
One-Dimensional Arrays
A one-dimensional array is, essentially, a list of like-typed
variables.
Obtaining an array is a two-step process:
1. Declare a variable of the desired array type.
2. Allocate the memory that will hold the array, using new, and assign it to
the array variable.
Step 1:
– General form of a one-dimensional array variable declaration is
type var-name[];
type declares the base type of the array.
– Example: array of int
int month_days[]; //Declaration
4
One-Dimensional Arrays
What we have got after Step 1?
– Although step 1 establishes the fact that month_days is an
array variable, no array actually exists.
– In fact, the value of month_days is set to null, which
represents an array with no value.
Step 2
– To link month_days with an actual, physical array of integers,
we allocate one using new and assign it to month_days.
– The general form of new as it applies to one-dimensional
arrays appears as follows:
– array-var = new type[size];
5
One-Dimensional Arrays
Step 2 (continues)
– To use new to allocate an array, we must specify the type and
number of elements to allocate
6
Example: One-Dimensional Arrays
// Demonstrate a one- month_days[5] = 30;
dimensional array. month_days[6] = 31;
class Array { month_days[7] = 31;
public static void main(String
month_days[8] = 30;
args[]) {
int month_days[]; month_days[9] = 31;
month_days = new int[12]; month_days[10] = 30;
month_days[0] = 31; month_days[11] = 31;
month_days[1] = 28; System.out.println("April
month_days[2] = 31; has " + month_days[3] +
" days.");
month_days[3] = 30;
}
month_days[4] = 31;
}
7
One-Dimensional Arrays
It is possible to combine the declaration of the array
variable with the allocation of the array itself
– int month_days[] = new int[12];
8
Multidimensional Arrays
Multidimensional arrays are actually arrays of
arrays
Example:
– int twoD[][] = new int[4][5];
9
A Conceptual View of a 4 by 5, 2D Array
[0] [0] [0] [1] [0] [2] [0] [3] [0] [4]
Left index
determines row [1] [0] [1] [1] [1] [2] [1] [3] [1] [4]
[2] [0] [2] [1] [2] [2] [2] [3] [2] [4]
[3] [0] [3] [1] [3] [2] [3] [3] [3] [4]
int twoD[][] = new int[4][5]; 10
Example: Multidimensional Arrays
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " "); Output
System.out.println();
} 01234
}
} 56789
10 11 12 13 14
15 16 17 18 19
11
Multidimensional Arrays
When we allocate memory for a multidimensional
array, we need only to specify the memory for the
first (leftmost) dimension.
We can allocate the remaining dimensions separately.
int twoD[][] = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];
12
Multidimensional Arrays
When we allocate dimensions manually, we don’t need
to allocate the same number of elements for each
dimension.
Since multidimensional arrays are actually arrays of
arrays, the length of each array is under our control.
We can create a two-dimensional array in which the
sizes of the second dimension are unequal.
13
Example: Multidimensional Arrays
// Manually allocate differing for(i=0; i<4; i++)
size second dimensions. for(j=0; j<i+1; j++) {
class TwoDAgain { twoD[i][j] = k;
public static void k++;
main(String args[]) { }
for(i=0; i<4; i++) {
int twoD[][] = new int[4]
for(j=0; j<i+1; j++)
[];
System.out.print(twoD[i]
twoD[0] = new int[1]; [j] + " ");
twoD[1] = new int[2]; System.out.println();
twoD[2] = new int[3]; }
twoD[3] = new int[4]; }
int i, j, k = 0; }
14
Multidimensional Arrays
It is possible to initialize multidimensional arrays when we
create them
Example
double m[][] = { We can use expressions
as well as literals inside
{ 0*0, 1*0, 2*0, 3*0 },
array initializers
{ 0*1, 1*1, 2*1, 3*1 },
};
15
Example: Multidimensional Arrays
// Demonstrate a three- for(i=0; i<3; i++) {
dimensional array. for(j=0; j<4; j++) {
class threeDMatrix { for(k=0; k<5; k++)
public static void main(String System.out.print(threeD[i][j][k]
args[]) { + " "); Output
int threeD[][][] = new System.out.println(); 0 0 0 0 0
0 0 0 0 0
int[3][4][5]; 0 0 0 0 0
} 0 0 0 0 0
int i, j, k; System.out.println();
0 0 0 0 0
for(i=0; i<3; i++) } 0 1 2 3 4
0 2 4 6 8
for(j=0; j<4; j++)
} 0 3 6 9 12
for(k=0; k<5; k++) } 0 0 000
0 2 468
threeD[i][j][k]=i * j * k; 0 4 8 12 16
0 6 1216
18 24
Alternative Array Declaration Syntax
17
Finding Length of an Array
An important point about Java arrays is that
18
Finding Length of an Array
// This program demonstrates the length array member.
class Length {
public static void main(String args[]) {
int a1[] = new int[10];
int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};
int a3[] = {4, 3, 2, 1};
Displaying 2D Arrays
– Column First order
– Row first order
20
Recommended Readings
Page # 48 to 56, Chapter # 3: Data Types,
Variables, and Arrays from Herbert Schildt,
Java: The Complete Reference, J2SETM 5 Edition
21