How to create a simple 4 in Java × 3 two dimensional array?
•
Java
I already know it in C, but Java is more challenging for me This is what I have I just want it to have 4 rows and 3 columns initialized to 1 - 12 and print it to the screen Is my mistake obvious to you? thank you!
I got 13 errors:(
Code I tried:
import java.util.*; class twoDimensional array { public static void main(String args[]) { int[][] twoDArray = new int[4][3]; twoDArray[][]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}}; System.out.print(twoDArray.toString); } }
Solution
First, arrays (even 2D arrays) do not overwrite object toString. You can use arrays Deeptostring (object []) and initialize the array at declaration time It's like
int[][] twoDArray = new int[][] { { 1,3 },{ 4,6 },{ 7,9 },{ 10,12 } }; System.out.println(Arrays.deepToString(twoDArray));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码