[英]Transposed array is not printing correctly
I'm trying to print the transposed version of this matrix array but it is instead printing the memory locations. 我正在尝试打印此矩阵数组的转置版本,但它正在打印内存位置。 Any input on how to print the actual array?
关于如何打印实际数组的任何输入?
This is what it prints: [[I@4b71bbc9, [I@17dfafd1, [I@5e8fce95] 它是这样打印的:[[I @ 4b71bbc9,[I @ 17dfafd1,[I @ 5e8fce95]
import java.util.*;
class MatrixTranspose
{
public static void main (String [] args)
{
int [] [] m = {
{3, 6, 9},
{4, 1, 5},
{5, 8, 7},
};
System.out.println (Arrays.toString (transpose(m)));
}
public static int [] [] transpose (int [] [] x)
{
int start = 1;
for (int r = 0; r < x.length; r++)
{
for (int c = start; c < x[0].length; c++)
{
int temp = x[r][c];
x [r][c] = x[c][r];
x[c][r] = temp;
}
start++;
}
return x;
}
}
transpose(int[][] x)
returns a 2-dimensional array. transpose(int[][] x)
返回一个二维数组。
Use Arrays.deepToString(Object[] a)
instead : 使用
Arrays.deepToString(Object[] a)
代替:
If an element e is an array of a primitive type, it is converted to a string as by invoking the appropriate overloading of
Arrays.toString(e)
.如果元素e是原始类型的数组,则通过调用
Arrays.toString(e)
的适当重载将其转换为字符串。 If an element e is an array of a reference type, it is converted to a string as by invoking this method recursively.如果元素e是引用类型的数组,则可以通过递归调用此方法将其转换为字符串。
System.out.println(Arrays.deepToString(transpose(m)));
Use Arrays#deepToString
instead. 请改用
Arrays#deepToString
。
In Java, each object has toString()
method, the default is displaying the class name representation, then adding @ and then the hashcode. 在Java中,每个对象都有
toString()
方法,默认是显示类名称表示形式,然后添加@,然后添加哈希码。
To better understand the output you're getting, see the implementation of Arrays#toString
: 为了更好地理解您获得的输出,请参见
Arrays#toString
的实现:
3860 public static String toString(int[] a) { {
3861 if (a == null)
3862 return "null";
3863 int iMax = a.length - 1;
3864 if (iMax == -1)
3865 return "[]";
3866
3867 StringBuilder b = new StringBuilder();
3868 b.append('[');
3869 for (int i = 0; ; i++) {
3870 b.append(a[i]);
3871 if (i == iMax)
3872 return b.append(']').toString();
3873 b.append(", ");
3874 }
3875 }
the toString
is applied on an array , resulting the "weird" output you got. toString
应用于数组 ,结果得到“奇怪”的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.