Talk about the difference between Arrays. deepToString of and Arrays. toString of

  • 2021-08-28 20:03:49
  • OfStack

Arrays. deepToString () is mainly used when there are arrays in an array, while Arrays. toString () is the opposite. For Arrays. toString (), when there are arrays in an array, the contents of the array will not be printed, only in the form of addresses.

Example:


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

Results


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

Additional: Arrays. deepToString () interpretation and usage (returns a string representation of the "deep content" of a specified array)

deepToString


public static String deepToString(Object[] a)

Package location:


java.util.Arrays.deepToString()

Return value:

Returns a string representation of the "deep contents" of the specified array.

Interpretation and usage:

If the array contains other arrays as elements, the string representation contains its contents, and so on. This method is designed to convert multidimensional arrays into strings.

String representation: The string representation consists of a list of elements of an array enclosed in square brackets ("[]"). Adjacent elements are separated by characters "," (comma with space). These elements are converted to strings by String. valueOf (Object) unless they are arrays of their own.

For example:


import java.util.Arrays;
/**
 * Arrays.deepToString() Method prints the 2 In the dimension array 1 Values in Dimensional Array 
 * Arrays.toString() Method prints the 2 In the dimension array 1 Address of dimension array 
 */
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]
 }
}

Print results:


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

Related articles: