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());
}
Answer from cl-r on Stack ExchangeTried printing out the rows in a 2d matrix using this code:
class Main {
public static void main(String args[]) {
int[][]matrix = { {1,2,3},
{4,5,6},
{7,8,9},
{1,2,1} };
for(int[] row: matrix){
System.out.println(row);
}
}
}It doesn't work. I get this on the compiler:
[I@76ed5528 [I@2c7b84de [I@3fee733d [I@5acf9800
What's going on?
The best way to print a Java 2D array? - Stack Overflow
Help with printing a two dimensional array
Is your issue with printing a matrix or table, or is your issue with the array index being out of bounds?
More on reddit.com[Java] Printing out a row in a 2d array
How to print a 2-D array with initial values in a table-like ...
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.
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));
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();
}
Hello everyone,
for a uni assignment I have to write a method that prints a grid view of a list of titles with three titles in a row. To achieve that, my idea is to first trim pr pad the titles so they have the same length, then create a two dimensional array with three titles in each subarray. Lastly, I want to loop through the array (of arrays) and print each subarray in one row. The padding and trimming works just fine, but there seems to be a problem with the creation of the 2D-array. When I try to print it, nothing happens. The code compiles just fine though. It looks like this:
public void printGridView()
{
// First, get all currently stored titles and trim them to size
String[] fittedTitles = new String[movies.size()];
String fittedTitle = "";
int index = 0;
for (Movie movie : movies) {
int len = movie.getTitleLength(movie.getTitle());
if (len > 21){
fittedTitle = movie.trimTitle(movie.getTitle());
} else if (len < 21) {
fittedTitle = movie.padTitle(movie.getTitle());
}
fittedTitles[index] = fittedTitle + " (" + movie.getYearOfRelease() + ")"; // add the year to the array for easier printing
index++;
}
// Next, create an array for each row of threes to be printed
int rest = fittedTitles.length % 3; // determine whether there is an array with less than 3 columns
int chunks = fittedTitles.length / 3 + (rest > 0 ? 1 : 0); //adds an extra array if there is a rest
String[][] arrays = new String[chunks][];
for (int i = 0; i < (rest > 0 ? chunks - 1 : chunks); i++) {
arrays[i] = Arrays.copyOfRange(fittedTitles, i * 3, i * 6); // Exception with copyOfRange: array building doesn't work correctly.
}
if (rest > 0){
arrays[chunks-1] = Arrays.copyOfRange(fittedTitles, (chunks-1) * 3, (chunks-1) * (3 + rest)); // makes an extra array with less than three titles
}
// quick test if array was built correctly
for (int i = 0; i < arrays.length; ++i) {
for(int j = 0; j < arrays[i].length; ++j) {
System.out.println(arrays[i][j]);
}
}
// Finally, print each row of threes accordingly
int i = 0;
if (rest == 0) { // in case of even rows of threes
for (i = 0; i < arrays.length; i++) {
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
System.out.println("|| || || ||");
System.out.println("|| " + arrays[i][0] + " || " + arrays[i][1] + " || " + arrays[i][2] + " ||");
System.out.println("|| || || ||");
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
}
} else if (rest == 2) { // in case of uneven rows with 2 left
String[] array = arrays[i];
while (i < (arrays.length-1)) {
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
System.out.println("|| || || ||");
System.out.println("|| " + array[0] + " || " + array[1] + " || " + array[2] + " ||");
System.out.println("|| || || ||");
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
i++;
}
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
System.out.println("|| || ||");
System.out.println("|| " + array[0] + " || " + array[1] + " ||");
System.out.println("|| || ||");
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
} else {
String[] array = arrays[i];
while (i < (arrays.length-1)) { // in case of uneven rows with one left
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
System.out.println("|| || || ||");
System.out.println("|| " + array[0] + " || " + array[1] + " || " + array[2] + " || ");
System.out.println("|| || || ||");
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
i++;
}
System.out.println("||||||||||||||||||||||||||||||||");
System.out.println("|| ||");
System.out.println("|| " + array[0] + " ||");
System.out.println("|| ||");
System.out.println("||||||||||||||||||||||||||||||||");
}
}When I try to print the 2D-array into the grid view, I get the exception
"java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0".
I have tried to recreate a case of 3 titles by hand and found no mistakes, I feel like it should work or at least give me the same errors for both ways of printing and I'm at my wits end.
Can somebody help please?
Is your issue with printing a matrix or table, or is your issue with the array index being out of bounds?
I would recommend making all of your print statements as simple as they can be to get a table to print. Once you can get a table to print, start adding in the other things you need. Google all your errors along the way. Hopefully this way you can pinpoint where the error is coming from more specifically and try to correct the problem that way.