有没有办法使用Arrays.deepToString()
从多维数组中打印特定值?
例如,我想在多维数组中打印索引 [1,1] 处的值。
我希望这是有道理的。
public class App {
public static void main(String[] args) {
int[][] a = {
{10,20,30,40,50},{10,20,30,40,50}
};
System.out.println(Arrays.deepToString(a));
System.out.println(Arrays.deepToString(a[1][1]));
}
}
System.out.println(a[1][1]);
指纹:
20
数组中的 deepToString(Object[]( 返回对象数组的字符串表示形式。 喜欢:-
int[][] a = {{10,20,30,40,50},{10,20,30,40,50}};
System.out.println(Arrays.deepToString(a));
Output:- [[10, 20, 30, 40, 50], [10, 20, 30, 40, 50]] // String representation of 'a'
deepToString(( 就是这个。
如果要打印特定元素,请使用数组的坐标,例如
System.out.println(a[1][2]); //Output:- 30
简答题
是的 它有效。 您在帖子的评论中指出了这一点。
为什么有效
这Arrays.deepToString
似乎递归扩展 (并调用toString
方法上的每个元素( 传递它的任何数组。 您的情况是特例 "不是数组,只需打印参数"。 在您的示例中, 第二次调用 deepToString 是不必要的。