You can print in simple way.
Use below to print 2D array
int[][] array = new int[rows][columns];
System.out.println(Arrays.deepToString(array));
Use below to print 1D array
int[] array = new int[size];
System.out.println(Arrays.toString(array));
Answer from Prabhakaran Ramaswamy on Stack OverflowYou can print in simple way.
Use below to print 2D array
int[][] array = new int[rows][columns];
System.out.println(Arrays.deepToString(array));
Use below to print 1D array
int[] array = new int[size];
System.out.println(Arrays.toString(array));
I would prefer generally foreach when I don't need making arithmetic operations with their indices.
for (int[] x : array)
{
for (int y : x)
{
System.out.print(y + " ");
}
System.out.println();
}
java - Printing a 2D array - Code Review Stack Exchange
Print 2D color array with boxes
[Java] Printing out a row in a 2d array
Trying to print a 2d array by columns.
Videos
Coding enhancement
public static int[][] getArray(){
return new int[5][6]; // no need of intermediate object
}
Optimizing speed : never have a computation or an evaluation at each loop, car be very heavy with great number
for(int i = 0, n = array.lenght; i < n ; i++) { ..
as init_array return array you can write directly :
printArray(init_array(getArray()));
Use :
System.getProperty("line.separator");for all OSStringBuilderthen print out :
.
public static void printArray(int[][] array){
String NEWLINE = System.getProperty("line.separator"); // You can put it 'public static' elsewhere
StringBuilder sb = new StringBuilder(); // you can give the size of it between ()
for(int i = 0; i< array.length;i++)
for(int j =0; j< array[i].length;j++)
sb.append(array[i][j])
sb.append(NEWLINE);
}
}
System.out.println(sb.toString());
}
You need to read input (from the command line) using the Scanner class for the dimensions of the array. In this line, you chose 5 and 6:
int[][] array = new int[5][6];
Everything looks good except for that method (getArray). In that method, you should read in two numbers from the command line, and use those numbers (instead of 5 and 6) as the dimensions of the array.
Here is my code that I found online.
public static void printMatrix(int size,int row, Color[][] array) {
for (int i = 0; i < 7 * size; i++) {
System.out.print("-");
}
System.out.println("-");
for (int i = 1; i <= array[row].length; i++) {
System.out.printf("| %4d ", array[row][i - 1]);
}
System.out.println("|");
if (row == size - 1) {
for (int i = 0; i < 7 * size; i++) {
System.out.print("-");
}
System.out.println("-");
}
}In my main method, I call the printMatrix function and define size, row, and the Color[][] array.
I receive the error:
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.awt.Color
at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4445)
at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2957)
at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2911)
at java.base/java.util.Formatter.format(Formatter.java:2689)
at java.base/java.io.PrintStream.format(PrintStream.java:1209)
at java.base/java.io.PrintStream.printf(PrintStream.java:1105)
at Main.printMatrix(Main.java:50)
at Main.main(Main.java:35)From googling things, I believe the issue is that %4d only works for strings. Is there something else I Can use for all types of array formats?
My end goal is for the output to be like this:
Where x is something like: java.awt.Color[r=255,g=0,b=0] (I'll remove the java.awt.Color part later). ---------------------- | x | x | x | ---------------------- | x | x | x | ---------------------- | x | x | x | ----------------------