溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Arrays.deepToString()與Arrays.toString()有什么區(qū)別

發(fā)布時(shí)間:2021-02-24 16:50:09 來源:億速云 閱讀:156 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)Arrays.deepToString()與Arrays.toString()有什么區(qū)別,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

Arrays.deepToString()主要用于數(shù)組中還有數(shù)組的情況,而Arrays.toString()則相反,對于Arrays.toString()而言,當(dāng)數(shù)組中有數(shù)組時(shí),不會(huì)打印出數(shù)組中的內(nèi)容,只會(huì)以地址的形式打印出來。

示例:

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));
 }
}

結(jié)果

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

補(bǔ)充:Arrays.deepToString()解釋和用法(返回指定數(shù)組“深層內(nèi)容”的字符串表示形式)

deepToString

public static String deepToString(Object[] a)

包位置:

java.util.Arrays.deepToString()

返回值:

返回指定數(shù)組“深層內(nèi)容”的字符串表示形式。

解釋與用法:

如果數(shù)組包含作為元素的其他數(shù)組,則字符串表示形式包含其內(nèi)容等。此方法是為了將多維數(shù)組轉(zhuǎn)換字符串而設(shè)計(jì)的。

字符串表現(xiàn)形式: 字符串表示形式由數(shù)組的元素列表組成,括在方括號(hào)("[]")中。相鄰元素用字符 ", "(逗號(hào)加空格)分隔。這些元素通過 String.valueOf(Object) 轉(zhuǎn)換為字符串,除非它們是自身的數(shù)組。

舉例說明:

import java.util.Arrays;
/**
 * Arrays.deepToString()方法打印的是二維數(shù)組中一維數(shù)組中的值
 * Arrays.toString()方法打印的是二維數(shù)組中一維數(shù)組的地址
 */
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]
 }
}

打印結(jié)果:

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

看完上述內(nèi)容,你們對Arrays.deepToString()與Arrays.toString()有什么區(qū)別有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI