2D Iterations and Repetition
Unit 08 2D Arrays
2D Arrays
A 2D array is an array of arrays, and a type of multidimensional array. To create a 2D array, you write the type of the class, followed by the name, and the values.
College Board
Unit 8 is worth 7.5-10% of the college board test, so it is not worth a huge amount. However, it is still important to understand how they work and when they can be utilized.
Learning Objectives
- 8.1 2D Arrays
- Represent collections of related primitive or object reference data using two-dimensional (2D) array objects.
- 8.2 Traversing 2D Arrays
- For 2D array objects:
- a. Traverse using nested for loops.
- b. Traverse using nested enhanced for loops.
- For 2D array objects:
int[][] numbers;
String[][] names;
char[][] letters;
float[][] floats;
double[][] doubles;
Object[][] objects;
int[][] numbers1 = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};//method 1:
int[][] numbers2 = new int[4][3]; //method 2: Creates array with four rows and 3 columns
String[][] names1 = {{"John","James","Jay"},{"Melissa","May","Maggie"},{"Bob","Burt","Billy"}}; //method 1
String[][] names2 = new String[2][2]; //method2: Creates array with 2 rows and 2 columns
char[][] letters1 = {{'a','b','c'},{'d','e','f','g','h'}}; //method 1
char[][] letters2 = new char[2][3]; //method 2: Creates array with 2 rows and 3 columns
Note, that when you initialize a 2D array using method 1, each row can have a different number of columns, ie, each array within the array can have a different number of values.
String[][] alphabet = {{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "="},
{"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\\"},
{"a", "s", "d", "f", "g", "h", "j", "k", "l"},
{"z", "x", "c", "v", "b", "n", "m", ",", ".", "/"}};
for(int i = 0;i<alphabet.length;i++){
for(int j = 0; j < alphabet[i].length;j++){ //nested for loops
System.out.print(alphabet[i][j]+" ");
}
System.out.println(" ");
}
In this way, you can print all the values of a certain 2D array.
int[][] numbers = new int[2][2]; //method 2: Creates array with two rows and two columns
numbers[0][0] = 1;
numbers[0][1] = 4;
numbers[1][0] = 9;
numbers[1][1] = 16;
for(int i = 0;i<numbers.length;i++){
for(int j = 0; j < numbers[i].length;j++){ //nested for loops
System.out.print(numbers[i][j]+" ");
}
System.out.println(" ");
}
int[][]products = new int [10][10]; //creating 2D Array
for(int i = 0;i<products.length;i++){
for(int j = 0; j < products[i].length;j++){
products[i][j] = i*j; //initializing values for array
}
}
System.out.println("Printing out values in a pattern/grid design:\n");
for(int i = 0;i<products.length;i++){
for(int j = 0; j < products[i].length;j++){
System.out.print(products[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("\nPrinting out values horizontally:\n");
for(int i = 0;i<products.length;i++){
for(int j = 0; j < products[i].length;j++){
System.out.print(products[i][j]+" ");
}
}
System.out.println("\n\nPrinting out values vertically:\n");
for(int i = 0;i<products.length;i++){
for(int j = 0; j < products[i].length;j++){
System.out.println(products[i][j]+" ");
}
}
int[][]sums = new int [10][10]; //creating 2D Array
for(int i = 0;i<sums.length;i++){
for(int j = 0; j < sums[i].length;j++){
sums[i][j] = i+j; //initializing values for array
}
}
System.out.println("Printing out values forward");
for(int i = 0;i<products.length;i++){
for(int j = 0; j < products[i].length;j++){
System.out.print(products[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("\n\nPrinting out values backward");
for(int i = products.length-1;i>=0;i--){
for(int j = products[i].length-1; j >= 0;j--){
System.out.print(products[i][j]+" ");
}
System.out.println(" ");
}
int[][]sums = new int [10][10]; //creating 2D Array
for(int i = 0;i<sums.length;i++){
for(int j = 0; j < sums[i].length;j++){
sums[i][j] = i+j; //initializing values for array
}
}
System.out.println("Rows forward, columns backward");
for(int i = 0;i<products.length;i++){
for(int j = products[i].length-1; j >= 0;j--){
System.out.print(products[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("\n\nRows backward, columns forward");
for(int i = products.length-1;i>=0;i--){
for(int j = 0; j <products[i].length;j++){
System.out.print(products[i][j]+" ");
}
System.out.println(" ");
}
- Create a class for 2D array learning.
- Create a method too initialize a 2D array with arbitrary values
- Create a method to reverse the 2D array and print out the values
- Create a method that asks for the input of a position and it returns the corresponding value
- Create a method that multiplies each value in a row and then adds all the products together
- Create a new object to test out each method in the main function