[Java beginner] About initialization of multidimensional array

When I was learning to get java Silver, I'm stuck at the initialization of the multidimensional array, so I'll leave it as a memo.

What i didn't understand

String[][] array = {{"a","b"},{"c","d","e"},{"f","g","h","i"}};

I wrote [] [] when declaring an array, but I was confused because the number of parentheses did not correspond to {} {} {}. I couldn't understand why this array would compile ...

How to read

It was a simple story.

String[][] array = {{"array[0][]"},{"array[1][]"},{"array[2][0]"}};

The three {} correspond to the subscript part of the first-dimensional array, The values of a to i described in {} were the values of the second-dimensional array referenced by the first-dimensional array.

public class Main{
	public static void main(String[] args) {
		String array[][] = {{"a","b"},{"c","d","e"},{"f","g","h","i"}};
		System.out.println(array[0][0]); //a
		System.out.println(array[0][1]); //b
		System.out.println(array[1][0]); //c
		System.out.println(array[1][1]); //d
		System.out.println(array[1][2]); //e
		System.out.println(array[2][0]); //f
		System.out.println(array[2][1]); //g
		System.out.println(array[2][2]); //h
		}
	}

When I actually output each element with the println method, the expected result was returned.

By the way, when actually outputting each element of the array,

public class Main {
	public static void main(String[] args) {
		String array[][] = { { "a", "b" }, { "c", "d", "e" }, { "f", "g", "h", "i" } };
		for(String[] tmp : array) {
			for(String s : tmp) {
				System.out.println(s);
			}
		}
	}
}

In this way, the method of turning an array using a for statement is more common.

Readability is not good for the 3rd and subsequent dimensions, but it is possible to nest and output each element as shown below.

public class Main {
	public static void main(String[] args) {
		String array[][][] = { { { "a", "b" }, { "c", "d", "e" } }, { { "f", "g", "h", "i" }, { "j", "k", "l", "m" } },
				{ { "n", "o", "p" }, { "q" } } };
		for (String[][] s1 : array) {
			for (String[] s2 : s1) {
				for (String s3 : s2) {
					System.out.println(s3);
				}
			}
		}
	}
}

Recommended Posts

[Java beginner] About initialization of multidimensional array
[Java Silver] About initialization
[Beginner] Java basic "array" description
About fastqc of Biocontainers and Java
About Lambda, Stream, LocalDate of Java8
[Java beginner] About abstraction and interface
[Java] Output multidimensional array / spreadsheet (AOJ⑥ spreadsheet)
Explanation about Array object of Ruby
[Java] array
Java array
Java array
java (array)
java beginner 4
Java array
[Java] Array
java beginner 3
java beginner
Java array
12 of Array
java array
[Java] Array
[Java] Multidimensional array / inner product calculation (AOJ⑦ inner product)
Compare the elements of an array (Java)
[Basic knowledge of Java] About type conversion
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
[Introduction to Java] About array operations (1D array, 2D array declaration, instantiation, initialization and use)
About Java interface
[Java] About Java 12 features
java array variable
About the description order of Java system properties
About the idea of anonymous classes in Java
[Java] Array notes
Something about java
Where about java
About Java features
About Java threads
[Java] About interface
Basics of threads and Callable in Java [Beginner]
About Java class
About Java arrays
About java inheritance
About interface, java interface
[Java] Overview of Java
About List [Java]
About java var
About Java literals
About Java commands
Java Exercise "Beginner"
Initialization of for Try to make Java problem TypeScript 5-4
[Java] About Objects.equals () and Review of String comparisons (== and equals)
About full-width ⇔ half-width conversion of character strings in Java
Predicted Features of Java
Java, about 2D arrays
[Java] Significance of serialVersionUID
About class division (Java)
About disconnect () of HttpURLConnection
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
Review of java Shilber
[Java] About Singleton Class
About selection of OpenJDK