欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

聊聊Arrays.deepToString()和Arrays.toString()的区别

程序员文章站 2022-07-03 07:58:01
arrays.deeptostring()主要用于数组中还有数组的情况,而arrays.tostring()则相反,对于arrays.tostring()而言,当数组中有数组时,不会打印出数组中的内容...

arrays.deeptostring()主要用于数组中还有数组的情况,而arrays.tostring()则相反,对于arrays.tostring()而言,当数组中有数组时,不会打印出数组中的内容,只会以地址的形式打印出来。

示例:

package com.oovever.hutool.util;
import java.util.arrays;
/**
* @author oovever
* @date 2017/12/24 17:31
*/
public class test {
 public static void main(string[] args) {
  int a[] = {1, 2, 3};
  system.out.println(arrays.tostring(a));
  int b[][] = {{1, 2, 3}, {4, 5, 6}};
  system.out.println(arrays.tostring(b));
  system.out.println(arrays.deeptostring(b));
 }
}

结果

[1, 2, 3]
[[i@14ae5a5, [i@7f31245a]
[[1, 2, 3], [4, 5, 6]]

补充:arrays.deeptostring()解释和用法(返回指定数组“深层内容”的字符串表示形式)

deeptostring

public static string deeptostring(object[] a)

包位置:

java.util.arrays.deeptostring()

返回值:

返回指定数组“深层内容”的字符串表示形式。

解释与用法:

如果数组包含作为元素的其他数组,则字符串表示形式包含其内容等。此方法是为了将多维数组转换字符串而设计的。

字符串表现形式: 字符串表示形式由数组的元素列表组成,括在方括号("[]")中。相邻元素用字符 ", "(逗号加空格)分隔。这些元素通过 string.valueof(object) 转换为字符串,除非它们是自身的数组。

举例说明:

import java.util.arrays;
/**
 * arrays.deeptostring()方法打印的是二维数组中一维数组中的值
 * arrays.tostring()方法打印的是二维数组中一维数组的地址
 */
public class testdeeptostring {
 public static void main(string[] args) {
  int[] array1 = {6, 6, 6};
  int[] array2 = {8, 8, 8};
  int[][] array3 = {array1, array2};
//  int[][] array4 = {{6, 6, 6}, {8, 8, 8}};
  system.out.println(arrays.deeptostring(array3)); //[[6, 6, 6], [8, 8, 8]]
  system.out.println(arrays.tostring(array3));  //[[i@511d50c0, [i@60e53b93]
 }
}

打印结果:

[[6, 6, 6], [8, 8, 8]]
[[i@511d50c0, [i@60e53b93]

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。