Created
October 6, 2020 07:30
-
-
Save FireController1847/80f00141c31fe2d63113acac3f0dc033 to your computer and use it in GitHub Desktop.
Multidimensional Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[] array = new int[] { 4, 6, 7, -2, 9 }; | |
for (int i = 0; i < 5; i++) { | |
System.out.println("(" + i + ") = " + array[i]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(0) = 4 | |
(1) = 6 | |
(2) = 7 | |
(3) = -2 | |
(4) = 9 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[][] array = new int[2][5]; | |
array[0] = { 4, 6, 7, -2, 9 }; | |
array[1] = { 5, 10, -2, 5, 2 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[][] array = { | |
{ 4, 6, 7, -2, 9 }, // Array 1 | |
{ 5, 10, -2, 5, 2 } // Array 2 | |
}; | |
// Loop through the first two arrays | |
for (int i = 0; i < 2; i++) { | |
// Loop through every value in the array at index [i] | |
for (int j = 0; j < 5; j++) { | |
System.out.println("(" + i + ", " + j + ") = " + array[i][j]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(0, 0) = 4 | |
(0, 1) = 6 | |
(0, 2) = 7 | |
(0, 3) = -2 | |
(0, 4) = 9 | |
(1, 0) = 5 | |
(1, 1) = 10 | |
(1, 2) = -2 | |
(1, 3) = 5 | |
(1, 4) = 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[][][] array = new int[2][2][2] | |
int[0] = { | |
{ 4, 6 }, | |
{ 5, 10 } | |
}; | |
int[1] = { | |
{ 7, 1 }, | |
{ 9, -1 } | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Computer screens are two dimensions, so we have to flatten the cube. | |
int[][][] array = { | |
{ | |
{ 4, 6 }, | |
{ 5, 10 } | |
}, | |
{ | |
{ 7, 1 }, | |
{ 9, -1 } | |
} | |
}; | |
// Select First Two-Dimensional | |
int[][] twoDimArray = array[0]; | |
// Select First Array { 4, 6 } | |
int[] firstArray = twoDimArray[0]; | |
// Or, you could do the follows | |
// int[] firstArray = array[0][0]; | |
// Loop through first array | |
for (int i = 0; i < 2; i++) { | |
System.out.println("(" + i + ") = " + firstArray[i]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(0) = 4 | |
(1) = 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I have flattened the numbers all the way to the first dimension (so a line of numbers). Don't mistake this, however, | |
// this array is still three-dimensions! | |
int[][][] array = {{{ 4, 6 }, { 5, 10 }}, {{ 7, 1 }, { 9, -1 }}}; | |
// Select Second Two-Dimensions | |
int[][] twoDimArray = array[1]; | |
// Loop through the two-dimensional array | |
for (int i = 0; i < 2; i++) { | |
for (int j = 0; j < 2; j++) { | |
System.out.println("(" + i + ", " + j + ") = " + twoDimArray[i][j]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(0, 0) = 7 | |
(0, 1) = 1 | |
(1, 0) = 9 | |
(1, 1) = -1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[][][] array = {{{ 4, 6 }, { 5, 10 }}, {{ 7, 1 }, { 9, -1 }}}; | |
// Use three for loops for three dimensions! | |
for (int i = 0; i < 2; i++) { | |
for (int j = 0; j < 2; j++) { | |
for (int k = 0; k < 2; k++) { | |
System.out.println("(" + i + ", " + j + ", " + k + ") = " + array[i][j][k]); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(0, 0, 0) = 4 | |
(0, 0, 1) = 6 | |
(0, 1, 0) = 5 | |
(0, 1, 1) = 10 | |
(1, 0, 0) = 7 | |
(1, 0, 1) = 1 | |
(1, 1, 0) = 9 | |
(1, 1, 1) = -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment