You are on page 1of 13

CHAPTER 7: ARRAYS OF

ARRAYS

PROGRAMMING 2
COMP 112
Introduction
2

— Java builds multi-dimensional arrays from many


one-dimensional arrays, it is called "arrays of arrays"
approach.

— There are some characteristics:


1. Rows may be different sizes.
2. Also, each row is an object (an array) that can be used
independently.

Programming 2 COMP 112 Spring 20-21


Creating two-dimensional Array – part 1
3

There are 3 ways to create two-dimensional array in java:


1. The short form declaration:
An easy way to define a two-dimensional array would be:
int[ ][ ] multiArr = {{1, 2}, {3, 4, 5}, {6, 7, 8, 9}};
We declared and initialized multiArr in a single step
2. Declaring and initialization with specifying the size of
rows and columns:
// Initialize Array elements more traditionally.
int[ ][ ] multiArr = new int[2][3];
// Initializing Array elements at position [0][0]
multiArr[0][0] = 15;
// Initializing Array elements at position [1][1]
multiArr[1][1] = 45;
// Initializing Array elements at position [2][1]
multiArr[2][1] = 65;
Programming 2 COMP 112 Spring 20-21
Creating two-dimensional Array – part 1 (Cont’d)
4

There are 3 ways to create two-dimensional array in java:


3. Declaration then initialization:
Declaring of the array with size 3 without mention the column
size. However, the Java compiler is intelligent enough to
calculate the size by checking the number of elements inside
the column.
int[ ][ ] multiArr = new int[3][ ];
multiArr[0] = new int[] {1, 2};
multiArr[1] = new int[] {3, 4, 5};
multiArr[2] = new int[] {6, 7, 8, 9};

Programming 2 COMP 112 Spring 20-21


Creating two-dimensional Array – part 2
5

— An array in Java is an object, the elements could be


either primitives or references. So, a two-dimensional
array in Java can be thought of as an array of one-
dimensional arrays.
— Our multiArr in memory would look similar to:
¡ multiArr[0] = new int[] {1, 2};
multiArr[0] is holding a reference to a single-dimensional array of
size 2.
¡ multiArr[1] = new int[] {3, 4, 5};
multiArr[1] holds a reference to another one-dimensional array of
size 3.
¡ multiArr[2] = new int[] {6, 7, 8, 9};
multiArr[2] holds a reference to another one-dimensional array of
size 4.
Programming 2 COMP 112 Spring 20-21
Creating two-dimensional array – part 3
6

In the above example: A two-dimensional array is created


with references to multi-sized one-dimensional arrays with
initial values or default value of ( 0).
Programming 2 COMP 112 Spring 20-21
Getting the dimensional length
7

The length of your “main” array in a two-dimensional array


can be obtained by referencing the following. We usually refer
to this as the number of “rows” in the array
list.length
● The length of each sub-array in a two- dimensional array can
be obtained by referencing the following. We usually refer to
this number as the “columns” in the array.
list[element].length
● Note that each row in a two
dimensional array could
have a different number of
columns
Programming 2 COMP 112 Spring 20-21
Iteration over two-dimensional array
8

— In order to iterate over all elements in a two-


•dimensional array you will need to maintain two
indexes – one for the row and one for the column.
¡ This is usually done by setting up a nested for loop like this:
for (int row = 0; row < list.length; row++) {
for (int col=0; col<list[row].length; row++)
{
//statements
// list[row][col] = …
}
}

Programming 2 COMP 112 Spring 20-21


Iteration over two-dimensional array
Example – part 1
9

int[][] a = {{1,2,3},
{4,5,6}};
for (int row=0; row < a.length; row++){
for (int col=0; col < a[row].length; col++){
a[row][col] = a[row][col] * 2;
}
}

[0] [1] [2]


a = [0] 1 2 3
[1] 4 5 6

Programming 2 COMP 112 Spring 20-21


Iteration over two-dimensional array
Example – part 2
10

— After finishing the iteration over the given two-dimensional


array, the result will be as following:

int[][] a = {{1,2,3},
{4,5,6}};
for (int row=0; row < a.length; row++){
for (int col=0; col < a[row].length; col++){
a[row][col] = a[row][col] * 2;
} [0] [1] [2]
} row = 2 a = [0] 2 4 6
[1] 8 10 12

Programming 2 COMP 112 Spring 20-21


Exception Handling
11

— Java defines several types of exceptions that relate to its


various class libraries. Java also allows users to define their
own exceptions.

— Built-in exceptions are the exceptions which are available in


Java libraries.
These exceptions are suitable to explain certain error
situations.

Programming 2 COMP 112 Spring 20-21


Exception Handling – part 2
12

● Below is the list of important built-in exceptions in Java used to avoid the
Error in the Array:
ArrayIndexOutOfBoundsException
o It is thrown to indicate that an array has been accessed with an illegal
index. The index is either negative or greater than or equal to the size
of the array.
class AOBExceptionTest {
public static void main(String args[]) {
try {
int a[]=new int[10];
//Array has only 10 elements
a[11] = 9;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println ("ArrayIndexOutOfBounds");
}
} // output: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
}
Programming 2 COMP 112 Spring 20-21
Exception Handling – part 3
13

NegativeArraySizeException:
— NegativeArraySizeException occurs whenever an
application tries to create an array with negative size.
— For example, when we assigned the negative size to an
array:
//Here the array size is -10:
//int[] arr = new int[-10];
public class NASExceptionTest {
public static void main(String[] args) {
int[] arr=new int[-10];
}
}
// output: Exception in thread "main“ java.lang.NegativeArraySizeException

Programming 2 COMP 112 Spring 20-21

You might also like