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

Arrays类中toString和deepToString的区别

程序员文章站 2024-03-06 22:30:44
...
  • Arrays.toString()当数组中有数组时,不会打印出数组中的内容,只会以地址的形式打印出来,返回一个包含数组元素的字符串

  • Arrays.deepToString()主要用于数组中包含数组(多维数组),会将内嵌数组的内容也打印出来

示例

import java.util.*;
import java.math.*;
public class Main{
	public static void main(String[] args) {
		
		String a1[]={"string1","string2","string3"};
		
		int[][] a2={
				{1,2,3,4},
				{5,6,7,8},
		};
		
		int[] a3 = {1,2,3};
		
		System.out.println(Arrays.toString(a1));
		System.out.println(Arrays.deepToString(a1));
		
		System.out.println(Arrays.toString(a2));
		System.out.println(Arrays.deepToString(a2));
		
		System.out.println(Arrays.toString(a3));
		//System.out.println(Arrays.deepToString(a3));  
		//会报错The method deepToString(Object[]) in the type Arrays is not applicable for the arguments (int[])
		//数组类型中的方法deepToString(Object [])不适用于参数(int [])
		//int基本数据类型,而String是Object的子类
	}
}

//输出
[string1, string2, string3]
[string1, string2, string3]
[[I@106d69c, [I@52e922]
[[1, 2, 3, 4], [5, 6, 7, 8]]
[1, 2, 3]