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 ] |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/mupengfei6688/article/details/78886050