0% found this document useful (0 votes)
9 views72 pages

CSE215 Ch7 8 Arrays MultiDArrays

The document covers the basics of arrays in programming, including their declaration, creation, and manipulation. It explains how to initialize arrays, access elements using indices, and the concept of passing arrays to methods. Additionally, it introduces the enhanced for loop for traversing arrays and discusses array copying techniques.
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)
9 views72 pages

CSE215 Ch7 8 Arrays MultiDArrays

The document covers the basics of arrays in programming, including their declaration, creation, and manipulation. It explains how to initialize arrays, access elements using indices, and the concept of passing arrays to methods. Additionally, it introduces the enhanced for loop for traversing arrays and discusses array copying techniques.
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

Chapter 7, 8

Array;
Multidimensional Array

CSE215: Programming Language II


Sec: 11
Faculty: Silvia Ahmed (SvA)
Summer 2025
Opening Problem
Read one hundred numbers, compute their
average, and find out how many numbers are
above the average.

Silvia Ahmed (SvA) CSE215: Programming Language II 2


Introducing Arrays
• Array is a data structure that represents a
collection of the same types of data.

Silvia Ahmed (SvA) CSE215: Programming Language II 3


Declaring Array Variables
• Syntax - 1:
datatype[] arrayRefVar;
• Example:
double[] myList;
• Syntax - 2:
datatype arrayRefVar[]; // This style is
allowed, but not preferred
• Example:
double myList[];

Silvia Ahmed (SvA) CSE215: Programming Language II 4


Creating Arrays
arrayRefVar = new datatype[arraySize];

Example:
myList = new double[10];

myList[0] references the first element in the array.


myList[9] references the last element in the array.

Silvia Ahmed (SvA) CSE215: Programming Language II 5


Declaring and Creating in One Step

• datatype[] arrayRefVar = new


datatype[arraySize];
double[] myList = new double[10];

• datatype arrayRefVar[] = new


datatype[arraySize];
double myList[] = new double[10];

Silvia Ahmed (SvA) CSE215: Programming Language II 6


The Length of an Array

• Once an array is created, its size is fixed. It


cannot be changed. You can find its size using

[Link]();

• For example,

[Link](); // returns 10

Silvia Ahmed (SvA) CSE215: Programming Language II 7


Default Values
• When an array is created, its elements are
assigned the default value of

– 0 for the numeric primitive data types,


– '\u0000' for char types, and
– false for boolean types.

Silvia Ahmed (SvA) CSE215: Programming Language II 8


Indexed Variables
• The array elements are accessed through the
index.
• The array indices are 0-based, i.e., it starts from
0 to [Link]-1.
• In the example in Slide 3, myList holds ten
double values and the indices are from 0 to 9.
• Each element in the array is represented using
the following syntax, known as an indexed
variable:
arrayRefVar[index];

Silvia Ahmed (SvA) CSE215: Programming Language II 9


Using Indexed Variables
• After an array is created, an indexed variable can
be used in the same way as a regular variable.
• For example, the following code adds the value
in myList[0] and myList[1] to myList[2].

myList[2] = myList[0] + myList[1];

Silvia Ahmed (SvA) CSE215: Programming Language II 10


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.

Silvia Ahmed (SvA) CSE215: Programming Language II 11


Shorthand Notation

double[] myList = {1.9, 2.9, 3.4, 3.5};

This shorthand notation is 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;

Silvia Ahmed (SvA) CSE215: Programming Language II 12


CAUTION
• Using the shorthand notation, you have to declare,
create, and initialize the array all in one statement.
• Splitting it would cause a syntax error.
• For example, the following is wrong:
double[] myList;

myList = {1.9, 2.9, 3.4, 3.5};

Silvia Ahmed (SvA) CSE215: Programming Language II 13


Trace Program with Arrays
Declare array variable values, create an
array, and assign its reference to values

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 14


Trace Program with Arrays
i becomes 1

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 15


Trace Program with Arrays
i (=1) is less than 5

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 16


Trace Program with Arrays
After this line is executed, value[1] is 1

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 17


Trace Program with Arrays
After i++, i becomes 2

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 18


Trace Program with Arrays
i (= 2) is less than 5

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 19


Trace Program with Arrays
After this line is executed,
values[2] is 3 (2 + 1)

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 20


Trace Program with Arrays
After this, i becomes 3.

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 21


Trace Program with Arrays
i (=3) is still less than 5.

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 22


Trace Program with Arrays
After this line, values[3] becomes 6 (3 + 3)

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 23


Trace Program with Arrays
After this, i becomes 4

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 24


Trace Program with Arrays
i (=4) is still less than 5

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 25


Trace Program with Arrays
After this, values[4] becomes 10 (4 + 6)

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 26


Trace Program with Arrays
After i++, i becomes 5

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 27


Trace Program with Arrays
i ( =5) < 5 is false. Exit the loop

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 28


Trace Program with Arrays
After this line, values[0] is 11 (1 + 10)

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 29


Processing Arrays
See the examples in the text.
1. (Initializing arrays with input values)
2. (Initializing arrays with random values)
3. (Printing arrays)
4. (Summing all elements)
5. (Finding the largest element)
6. (Finding the smallest index of the largest element)
7. (Random shuffling)
8. (Shifting elements)

Silvia Ahmed (SvA) CSE215: Programming Language II 30


Enhanced for Loop (for-each loop)
• JDK 1.5 introduced a new for loop that enables you to traverse the
complete array sequentially without using an index variable. For
example, the following code displays all elements in the array myList:

for (double value: myList)


[Link](value);

• In general, the syntax is

for (elementType value: arrayRefVar) {


// Process the value
}

• You still have to use an index variable if you wish to traverse the array in
a different order or change the elements in the array.

Silvia Ahmed (SvA) CSE215: Programming Language II 31


Copying Arrays
Often, in a program, you need to duplicate an array or a part
of an array. In such cases you could attempt to use the
assignment statement (=), as follows:

list2 = list1;

Silvia Ahmed (SvA) CSE215: Programming Language II 32


Copying Arrays

Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new
int[[Link]];

for (int i = 0; i < [Link]; i++)


targetArray[i] = sourceArray[i];

Silvia Ahmed (SvA) CSE215: Programming Language II 33


The arraycopy Utility

arraycopy(sourceArray, src_pos,
targetArray, tar_pos, length);

Example:
[Link](sourceArray, 0,
targetArray, 0, [Link]);

Silvia Ahmed (SvA) CSE215: Programming Language II 34


Passing Arrays to Methods
public static void printArray(int[] array) {
for (int i = 0; i < [Link]; i++) {
[Link](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

Silvia Ahmed (SvA) CSE215: Programming Language II 35


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

creates an array using the following syntax:


new dataType[]{literal0, literal1, ..., literalk};

• There is no explicit reference variable for the array.


Such array is called an anonymous array.

Silvia Ahmed (SvA) CSE215: Programming Language II 36


Pass By Value
• Java uses pass by value to pass arguments to a
method. There are important differences between
passing a value of variables of primitive data types and
passing arrays.

• For a parameter of a primitive type value, the actual


value is passed. Changing the value of the local
parameter inside the method does not affect the value
of the variable outside the method.

• For a parameter of an array type, the value of the


parameter contains a reference to an array; this
reference is passed to the method. Any changes to the
array that occur inside the method body will affect the
original array that was passed as the argument.
Silvia Ahmed (SvA) CSE215: Programming Language II 37
Simple Example
public class Test {
public static void main(String[] args) {
int x = 1; // x represents an int value
int[] y = new int[10]; // y represents an array of int values

m(x, y); // Invoke m with arguments x and y

[Link]("x is " + x);


[Link]("y[0] is " + y[0]);
}

public static void m(int number, int[] numbers) {


number = 1001; // Assign a new value to number
numbers[0] = 5555; // Assign a new value to numbers[0]
}
}

Silvia Ahmed (SvA) CSE215: Programming Language II 38


Trace the reverse Method
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
Declare result and create array
public static int[] reverse(int[] list) {
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 0

Silvia Ahmed (SvA) CSE215: Programming Language II 39


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 0 and j = 5
public static int[] reverse(int[] list) {
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 0

Silvia Ahmed (SvA) CSE215: Programming Language II 40


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (= 0) is less than 6
public static int[] reverse(int[] list) {
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 0

Silvia Ahmed (SvA) CSE215: Programming Language II 41


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 0 and j = 5
public static int[] reverse(int[] list) { Assign list[0] to result[5]
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 1

Silvia Ahmed (SvA) CSE215: Programming Language II 42


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);

After this, i becomes 1 and


public static int[] reverse(int[] list) { j becomes 4
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 1

Silvia Ahmed (SvA) CSE215: Programming Language II 43


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);

i (=1) is less than 6


public static int[] reverse(int[] list) {
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 0 1

Silvia Ahmed (SvA) CSE215: Programming Language II 44


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 1 and j = 4
public static int[] reverse(int[] list) { Assign list[1] to result[4]
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 45


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 2 and
public static int[] reverse(int[] list) { j becomes 3
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 46


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=2) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 0 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 47


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 2 and j = 3
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 48


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 3 and
public static int[] reverse(int[] list) { j becomes 2
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 49


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=3) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 0 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 50


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 3 and j = 2
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 4 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 51


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 4 and
public static int[] reverse(int[] list) { j becomes 1
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 4 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 52


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=4) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 0 4 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 53


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 4 and j = 1
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 5 4 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 54


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 5 and
public static int[] reverse(int[] list) { j becomes 0
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 5 4 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 55


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=5) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 0 5 4 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 56


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 5 and j = 0
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 6 5 4 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 57


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 6 and
public static int[] reverse(int[] list) { j becomes -1
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 6 5 4 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 58


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=6) < 6 is false. So exit
public static int[] reverse(int[] list) { the loop.
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

result 6 5 4 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 59


Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
Return result
public static int[] reverse(int[] list) {
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


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

return result;
}

list 1 2 3 4 5 6

list2
result 6 5 4 3 2 1

Silvia Ahmed (SvA) CSE215: Programming Language II 60


Chapter 8
Multidimensional Arrays

Silvia Ahmed (SvA) CSE215: Programming Language II 61


Motivations
Thus far, you have used one-dimensional arrays to model
linear collections of elements. You can use a two-dimensional
array to represent a matrix or a table. For example, the
following table that describes the distances between the cities
can be represented using a two-dimensional array.

Silvia Ahmed (SvA) CSE215: Programming Language II 62


Declare/Create 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];

Silvia Ahmed (SvA) CSE215: Programming Language II 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 < [Link]; i++)


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

double[][] x;

Silvia Ahmed (SvA) CSE215: Programming Language II 64


Two-dimensional Array Illustration

[Link]? 5 [Link]? 4
matrix[0].length? 5 array[0].length? 3

Silvia Ahmed (SvA) CSE215: Programming Language II 65


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];
{1, 2, 3}, array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;
{4, 5, 6}, Same as array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;
{7, 8, 9}, array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;
{10, 11, 12} array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
};

Silvia Ahmed (SvA) CSE215: Programming Language II 66


Lengths of Two-dimensional Arrays

int[][] x = new int[3][4];

Silvia Ahmed (SvA) CSE215: Programming Language II 67


Lengths of Two-dimensional Arrays, cont.

int[][] array =
[Link]
{
{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

Silvia Ahmed (SvA) CSE215: Programming Language II 68


Ragged Arrays
• Each row in a two-dimensional array is itself an
array. So, the rows can have different lengths. Such
an array is known as a ragged array. For example,
int[][] matrix = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5}, [Link] is 5
matrix[0].length is 5
{3, 4, 5},
matrix[1].length is 4
{4, 5}, matrix[2].length is 3
{5} matrix[3].length is 2
}; matrix[4].length is 1

Silvia Ahmed (SvA) CSE215: Programming Language II 69


Ragged Arrays, cont.

Silvia Ahmed (SvA) CSE215: Programming Language II 70


Processing Two-Dimensional Arrays

See the examples in the text.


1. (Initializing arrays with input values)
2. (Printing arrays)
3. (Summing all elements)
4. (Summing all elements by column)
5. (Which row has the largest sum)
6. (Finding the smallest index of the largest element)
7. (Random shuffling)

Silvia Ahmed (SvA) CSE215: Programming Language II 71


Multidimensional Arrays
• Occasionally, you will need to represent n-dimensional data
structures. In Java, you can create n-dimensional arrays for
any integer n.
• The way to declare two-dimensional array variables and
create two-dimensional arrays can be generalized to declare
n-dimensional array variables and create n-dimensional
arrays for n >= 3.
• For example, the following syntax declares a three-
dimensional array variable scores, creates an array, and
assigns its reference to scores.

double[][][] scores = new double[10][5][2];

Silvia Ahmed (SvA) CSE215: Programming Language II 72

You might also like