Lecture 03 - Arrays
Lecture 03 - Arrays
Programming
Lecture # 03: Arrays
Muhammad Imran
(Based on Java, The Complete Reference)
http://www.secscourses.tk
1
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.
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
7
One-Dimensional Arrays
8
One-Dimensional Arrays (An Example)
9
One-Dimensional Arrays (An Example)
int first = 9;
int second = first;
System.out.println(second);
first = 10;
System.out.println(second);
10
Multidimensional Arrays
• Multidimensional arrays are actually arrays of arrays
• Example:
int twoD[][] = new int[4][5];
11
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];
12
A Conceptual View of a 3 by 4, 2D Array
13
A Conceptual View of a 5 by 2, 2D Array
14
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++;
} Output
for(i=0; i<4; i++) { 01234
for(j=0; j<5; j++)
56789
System.out.print(twoD[i][j] + " ");
System.out.println(); 10 11 12 13 14
} 15 16 17 18 19
}
} 15
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];
16
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.
17
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++)
twoD[0] = new int[1];
System.out.print(twoD[i][j]
twoD[1] = new int[2]; + " ");
twoD[2] = new int[3]; System.out.println();
twoD[3] = new int[4]; }
int i, j, k = 0; }
}
18
Multidimensional Arrays
• It is possible to initialize multidimensional arrays when we create
them
• Example
double m[][] = {
We can use expressions
{ 0*0, 1*0, 2*0, 3*0 }, as well as literals inside
array initializers
{ 0*1, 1*1, 2*1, 3*1 },
};
19
3 Dimensional Arrays
20
3 Dimensional Arrays Conceptual View
21
Example: 3D 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(); 00000
00000
int[3][4][5]; } 00000
00000
int i, j, k; System.out.println();
for(i=0; i<3; i++) }
00000
01234
for(j=0; j<4; j++) }
02468
0 3 6 9 12
for(k=0; k<5; k++) } 00000
02468
threeD[i][j][k]=i * j * k; 0 4 8 12 16
0 6 12 18 24
22
Alternative Array Declaration Syntax
• There is a second form that may be used to declare an
array:
type[] var-name;
• The square brackets follow the type specifier, and not the
name of the array variable.
• The following two declarations are equivalent:
int a1[] = new int[3];
int[] a1 = new int[3];
23
Finding Length of an Array
• An important point about Java arrays is that
they are implemented as objects
• All arrays have this variable, and it will always hold the size
of the array
24
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
26
Recommended Readings
• Page # 51 to 58, Chapter # 3: Data Types, Variables, and
Arrays from Herbert Schildt, Java: The Complete
Reference, J2SETM 9th Edition
27