0% found this document useful (0 votes)
14 views27 pages

Lecture 03 - Arrays

The document discusses different types of arrays in Java including one-dimensional, multidimensional, and 3D arrays, how to declare, initialize, and access elements in arrays, and provides examples of declaring and using one-dimensional, two-dimensional, and three-dimensional arrays to demonstrate these concepts.

Uploaded by

Muhammad Tayyab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views27 pages

Lecture 03 - Arrays

The document discusses different types of arrays in Java including one-dimensional, multidimensional, and 3D arrays, how to declare, initialize, and access elements in arrays, and provides examples of declaring and using one-dimensional, two-dimensional, and three-dimensional arrays to demonstrate these concepts.

Uploaded by

Muhammad Tayyab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

SE241: Advanced Computer

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.

• 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

 The elements in the array allocated by new will automatically be initialized to


zero

 month_days = new int[12];

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 month_days[8] = 30;
main(String args[]) { month_days[9] = 31;
int month_days[]; month_days[10] = 30;
month_days = new int[12]; month_days[11] = 31;
month_days[0] = 31; System.out.println("April
month_days[1] = 28; has " + month_days[3] + "
month_days[2] = 31; 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];

• Arrays may be initialized when they are declared using an


array initializer list
 int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

8
One-Dimensional Arrays (An Example)

int[] test = {0,1,2,3,4,5};


int[] before = test;
System.out.println(before[2]);
test[2] = 65;
System.out.println(before[2]);

What will be the output?

9
One-Dimensional Arrays (An Example)

int first = 9;
int second = first;
System.out.println(second);
first = 10;
System.out.println(second);

What will be the output?

10
Multidimensional Arrays
• Multidimensional arrays are actually arrays of arrays

• Example:
 int twoD[][] = new int[4][5];

 This allocates a 4 by 5 array and assigns it to twoD. Internally this


matrix is implemented as an array of arrays of int.

11
A Conceptual View of a 4 by 5, 2D Array

Right index determines column

[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 },

{ 0*2, 1*2, 2*2, 3*2 },

{ 0*3, 1*3, 2*3, 3*3 }

};

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

• Because of this, there is a special array instance variable


length that stores size/length of an array

• 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};

System.out.println("length of a1 is " + a1.length);


System.out.println("length of a2 is " + a2.length);
System.out.println("length of a3 is " + a3.length);
} Output
} length of a1 is 10
length of a2 is 8
length of a3 is 4
25
Programming Exercises
• Matrix Manipulation
 Addition of two matrices
 Subtraction of two matrices
 Multiplication of two matrices (Hard)

• 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

You might also like