+ 2
why it's printing weired output if i do not specified the column while printing array elements ?
why it's printing symbols? https://code.sololearn.com/c4XqyornzZ41/?ref=app
3 Answers
+ 4
The weird output is because an Array's toString() returns memory reference information instead of the values from the array. arr[row] is an array. It is not an int.
You almost definitely want:
System.out.print(" "+arr[row][col]);
Instead of:
System.out.print(" "+arr[row]);
Here is the adjusted code:
public class Main {
public static void main(String[] args) {
int arr[][]={
{1,2,3,4},
{5,6,7},
{9,10,11,12}
};
for(int row=0;row<arr.length;row++)
{
for(int col=0;col<arr[row].length;col++)
{
System.out.print(" "+arr[row][col]);
}
System.out.println();
}
}
}
+ 3
Ratnapal wrote, "toString() is the Method of instance arr ?
what is memory reference information?"
Response:
You're getting something this printed: [I@54bedef2.
"[" is for array. Only one bracket because it is a 1-dimensional array that you're printing. arr is a 2 dimensional array but arr[row] is 1-dimensional.
"I" is for int. The array you're printing is of int.
@54bedef2 is related to memory address. A memory address is something you don't need to deal with directly in Java but you'd deal with them directly in c, c++, assembly and other older languages. The digits range from 0-9 and a-f because it is hexidecimal. It isn't base 10 like you almost always use in math class.
You saw that I gave you a fixed version of the code in the previous answer, right?
0
toString() is the Method of instance arr ?
what is memory reference information? đ€