Reputation: 87
We have been asked to print this 2D Array with the columns as rows
For example: the first column is 20,11,27 and it has to be printed:
20
11
27
Here's my code so far and I can't even get it printing the columns normally, do any of you guys know what the problem is and if you can help me find a solution to the problem?
public class TwoDimensionalArrays
{
public static void main (String args[])
{
final int size1 = 2, size2 = 4, size3 = 5;
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};
int row = 0, col = 0;
for(row = 0; row <= size1; row++); //loops through rows
{
for(col = 0; col <= size2; col++); //loops through columns
{
System.out.println(numbers[row][col]);
}
System.out.print("\n"); //takes a new line before each new print
}
}
}
Upvotes: 0
Views: 17373
Reputation: 1
public class rwr {
public static void main(String[] args){
final int size1=2,size2=4,size3=5;
int[][] number={{34,34,33,33,44},{22,23,24,23,24},{23,44,55,66,66}};
int row=0,col=0;
for(row=0;row<=size1;row++){
for(col=0;col<=size2;col++){
System.out.print(number[row][col]+"\t");
}
System.out.print(" \n");
}
}
}
run:
34 34 33 33 44
22 23 24 23 24
23 44 55 66 66
BUILD SUCCESSFUL (total time: 0 seconds)
Upvotes: 0
Reputation: 6242
You shouldn't rely on some predefined sizes of the multidimensional arrays(better name is array of arrays). Always use the real size of the array like numbers.length
and numbers[0].length
or use for-each like this:
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};
for (int[] row: numbers){
for (int num: row){
System.out.print(num+" ");
}
System.out.println();
}
Result is this:
20 25 34 19 33
11 17 15 45 26
27 22 9 41 13
If you want that transposed, you could do it like this:
for(int i = 0; i < numbers[0].length; i++) {
for(int j = 0; j < numbers.length; j++) {
System.out.print(numbers[j][i]+"\t");
}
System.out.println();
}
Now the result is:
20 11 27
25 17 22
34 15 9
19 45 41
33 26 13
Note: There's nothing like rows and columns in the array of arrays, only dimensions.
Upvotes: 1
Reputation: 5638
Delete ;
in the end of the loops
like this :
for (row = 0; row <= size1; row++) //loops through rows
{
for (col = 0; col <= size2; col++) //loops through columns
{
System.out.print(numbers[row][col]+" ");
}
System.out.print("\n"); //takes a new line before each new print
}
The Output :
20 25 34 19 33
11 17 15 45 26
27 22 9 41 13
Upvotes: 3
Reputation: 88707
You don't need to provide the sizes directly but you might have to calculate (or, to make it simpler, provide) the maximum length of an element.
Printing could then look like this:
int maxDigits = 2; //provided or calculated
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};
for( int[] row : numbers ) {
for( int n : row) {
System.out.print( String.format("%" + maxDigits + "d ", n) ); //creates the format string "%2d" for 2 digits maximum
}
System.out.println(); //takes a new line before each new print
}
Note that you should use System.out.print()
for printing without linebreaks.
The output would then look like this:
20 25 34 19 33
11 17 15 45 26
27 22 9 41 13
Upvotes: 0