---
description: In this lab, we are going to talk about multidimensional arrays, which we've talked about in the previous lab, the concept of multidimensional arrays is that the elements of an array are also an array!
---
<h1 style='border: none'><center>Programming I Lab 9</center></h1>
<h2 style='border: none'><center>Multidimensional Arrays</center></h2>
<h5><center>The Islamic University of Gaza<br>Engineering Faculty<br>Department of Computer Engineering</center></h5>
<h6>Author: Mohammed Nafiz ALMadhoun<span style="float:right">2021/11/26</span></h6>
---
<p style='text-align:justify'>
In this lab, we are going to talk about multidimensional arrays, which we've talked about in the previous lab, the concept of multidimensional arrays is that the elements of an array are also an array!
</p>
## Lab Objectives
- Understand the multidimensional arrays.
- Understand the concept of indices more.
## Multidimensional Arrays
### Array of arrays
To create an array of ints we will write:
```java=
int[] array = new int[10];
```
So the type of each element in the array is `int`, to create an array in which the element type is an array of ints `int[]`, we could simply write:
```java=
int[][] arrayOfArrays = new int[10][];
```
Note that in the array of ints, the value of each element is `zero`, but in the array of arrays the value of each element is `null`, until we've assigned a value to that element
```java=
System.out.println(arrayOfArrays[0]); // This will print `null`
```
As in the regular array we could assign the value to an element using its index:
```java=
arrayOfArrays[0] = new int[] {10, 20, 30};
arrayOfArrays[1] = new int[] {6, 12};
```
Notice that the element is an array, so we will put an array in it!
### Matrix
The only difference here is that a matrix will have an array of arrays, but in one dimension, all the arrays will have the same number of elements:
```java=
int[][] matrix = new int[2][3];
```
In this syntax, we will reserve memory for the array, and each child array, so if we printed the first element it will not be `null`:
```java=
System.out.println(matrix[0]); // This will print the address of it!
```
So we don't need to initialize each child array like the previous example.
### Printing all the elements of 2-Dimintaional Array
We can print all the elements of the array using two loops, in each iteration of the outer loop, we will get a child array, and in the inner loop, we will get the element in the child array.
```java=
public static void printMatrix (int[][] matrix) {
for (int i=0;i<matrix.length;i++) {
int[] childArray = matrix[i];
for (int j=0;j<childArray.length;j++) {
System.out.print(childArray[i] + " ");
}
System.out.println();
}
}
```
## Lab Tasks
### Task 1: Multiple Choice Quiz App
In this task, you should create a quizzer! the app should read a CSV file that contains the questions, and the answers, note that **the first answer in the file is the correct one**.
You should display 3 questions randomly without **repeating**, and the answers should be displayed randomly.
To randomize the questions and answers without repeating, you could return to Listing 7.2 in your textbook and create an array of indices then randomize it.
So you can write a method like this:
```java=
public static int[] generateRandomIndices (int length)
```
That returns random ints from `0` to `length-1`.
Use methods in your program to make your life easier, for example, create a method to read the file, and a method for asking a question.
<center>

Task 1 Output Example
</center>
Quiz Example CSV File:
```
Question,A,B,C,D
The main method should be written as:,public static void main(String[] args),public static void main(),public static main(),public void main(String[] args)
Your TA name is:,Mohammed ALMadhoun,Ahmed ALMadhoun,Mohammed Maliha,Nafiz ALMadhoun
JRE Stands for:,Java Runtime Environment,Java Run Exe,Java Run External,Java Run Execute
JDK Stands for:,Java Development Kit,Java Developer Kitten,Java Day Karate,Java Dead KO
To Create an int array we will write:,int[] array,[]int array,int[int] array,int array
```
###### tags: `Programming` `Java` `IUG`
<center>End Of Lab 9</center>