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

2025-05-29 0 144

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

示例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
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
[1, 2, 3]

[[I@14ae5a5, [I@7f31245a]

[[1, 2, 3], [4, 5, 6]]

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

deepToString

?

1
public static String deepToString(Object[] a)

包位置:

?

1
java.util.Arrays.deepToString()

返回值:

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

解释与用法:

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

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

举例说明:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
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]

}

}

打印结果:

?

1

2
[[6, 6, 6], [8, 8, 8]]

[[I@511d50c0, [I@60e53b93]

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

原文链接:https://blog.csdn.net/mupengfei6688/article/details/78886050

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 聊聊Arrays.deepToString()和Arrays.toString()的区别 https://www.kuaiidc.com/108398.html

相关文章

发表评论
暂无评论