2D Array Vocab:
- Array = a data structure used to implement a collection (list) of primitive or object reference data
- Element = a single value in the array
- Index = the position of the element in the array (starts from 0)
- Array Length = the number of elements in the array
- Is public, so can be accessed in any class
- Is also final, so can’t change it after array has been created
The Basics:
- A 2D array is an array of arrays, and can be a better way to store data
- Declaring a 2D array:
DataType[][] nameOf2DArray
- Initializing a 2D array
DataType[][] nameOf2DArray = new DataType[r][c];
- r = # of rows
- The # of arrays in the array
r = list.length
- c = # of columns
- The # of elements in the inner arrays
c = list[0].length
- r = # of rows
Initializing a Sample Array:
public class Test {
public static void main(String[] args) {
int[][] arr = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
System.out.println("arr[0][0] = " + arr[0][0]);
System.out.println("arr[1][2] = " + arr[1][2]);
System.out.println("arr[2][1] = " + arr[2][1]);
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "a", "f", "g" },
{ "b", "e", "h" },
{ "c", "d", "i" }
};
// Print the last element in the array!
System.out.println(arr[2][2]);
}
}
Test.main(null);
- a quick tip for the future:
list[list.length - 1][list[0].length - 1]
- Updating an element:
list[r][c] = value;
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
// Change Austin to Athens and print!
System.out.println("Change Austin to Athens and print!");
arr[2][0] = "Athens";
System.out.println(arr[2][0]);
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "a", "f", "g", "l" },
{ "b", "e", "h", "k" },
{ "c", "d", "i", "j" }
};
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 4; col++) {
System.out.print(arr[row][col] + " ");
}
System.out.println(" ");
}
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
// Print out the array without using numerical values!
for (String[] row: arr){
for (String col: row){
System.out.println(col);
}
}
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
String match = "";
String name = "Boston";
for (String[] row : arr) {
for (String item : row) {
if (item.equals(name)) {
match = name;
}
}
}
if (match.length() == 0) {
System.out.println("No Match!");
} else {
System.out.println(name);
}
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
String longest = arr[0][0];
// Use nested for loops to find the longest or shortest string!
System.out.println("Use nested for loops to find the longest or shortest string!");
String word = arr[0][0];
for (String[] row: arr){
for (String col: row){
if (word.length() < col.length()){
word = col;
}
}
}
System.out.println(word);
}
}
Test.main(null);
public class Tree {
public static void main(String[] args) {
// print a tree
int value = 5;
System.out.println(" *");
for(int k =0; k<2; k++){
for (int i = 1; value > i; i++){
for(int space = value - i; space>0; space--){
System.out.print(" ");
}
for(int a = 1; a<4*i ; a++){
System.out.print("*");
}
System.out.println("");
}
}
for(int j = 1; j<3; j++){
System.out.println(" |||");
}
}
}
Tree.main(null);
public class Position {
public static int arr[][] = {
{15, 5, 9, 10},
{12, 16, 11, 6},
{14, 8, 13, 7}
};
public int x;
public int y;
public Position(int x, int y){
this.x = x;
this.y = y;
}
public static int row = arr.length;
public static int col = arr[0].length;
public static Position findPosition(int num, int[][] intArr){
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(num == arr[i][j]){
return new Position(i, j);
}
}
}
return null;
}
public static Position[][] getSuccessorArray(int[][] intArr) {
Position[][] posStringArr = new Position[intArr.length][intArr[0].length];
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
posStringArr[i][j] = findPosition(intArr[i][j]+1, intArr);
}
}
return posStringArr;
}
public static void main(String[]args){
Position pos = findPosition(9, arr);
System.out.println("Row: " + pos.x + "\nColumn: " + pos.y);
System.out.println(pos.getSuccessorArray(arr));
}
}
Position.main(null);