Page 262 - PowerPoint Presentation
P. 262
CAVITE STATE UNIVERSITY
TRECE MARTIRES CITY CAMPUS
Department of Information Technology DCIT 111 - Advanced Programming
Double or Multi-Dimensional Array
In java, you can also take multidimensional arrays as arrays of arrays. You can declare
a multidimensional array variable by specifying each additional index with a set of square
brackets. In such case, data is stored in row and column-based index (also known as matrix
form).
// Array Declarations
1 2 3
Float twoDArr[] [] = new float[3][3]; // this is a 3x3 matrix 1 2 3
1 2 3
// Example of Multi-dimensional Array
Class MDArr {
public static void main (String[]args) {
int arr[] [] ={ {1, 2, 3}, {4, 5, 6}, {7, 8, 9});
for (int i=0; i<3; i++) {
for (int j=0; i<3; i++){
System.out.println(arr[i][j]+ “ “); OUTPUT:
} 1 2 3
} 4 5 6
} } 7 8 9
// Another Example of Multi-dimensional Array
import java.util.Scanner;
public class MDArr2 {
public static void main (String[]args) { OUTPUT:
Scanner sc = new Scanner(System.in);
Enter 9 numbers:
int arr[][] = new int[3][3]; 1
2
System.out.println(“Enter 9 numbers: “); 3
for (int i=0; i<3; i++) { 4
for (int j=0; j<3; j++) { 5
arr[i][j] = sc.nextInt(); 6
} 7
} 8
for (int i=0; i<3; i++) { 9
1 2 3
for (int j=0; j<3; j++) { 4 5 6
System.out.print(arr[i][j]+ “ “); 7 8 9
}
System.out.println();
}
} }
QUIZ NO. 4: Hardcoding: Write a program in a piece of paper that the computer will ask the
user to enter 20 numbers and then the program should display all the even and odd numbers
separately.
Odd Numbers are: ____________
Even Numbers are: ___________
38