Java Arrays.deepToString()用法及代码示例
2021/8/16 20:36:10
本文主要是介绍Java Arrays.deepToString()用法及代码示例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
java.util.Arrays.deepToString(Object [])方法是一个java.util.Arrays类方法。
返回指定数组的“deep contents”的字符串表示形式。如果数组包含其他数组作为元素,则字符串表示形式包含其内容,依此类推。此方法旨在将多维数组转换为字符串。简单的toString()方法适用于简单数组,但不适用于多维数组。此方法旨在将多维数组转换为字符串。
用法:
public static String deepToString(Object[] arr) arr - An array whose string representation is needed This function returns string representation of arr[]. It returns "null" if the specified array is null.
例:
Let us suppose that we are making a 2-D array of 3 rows and 3 column. 2 3 4 5 6 7 2 4 9 If use deepToString() method to print the 2-D array, we will get string representation as :- [[2,3,4], [5,6,7], [2,4,9]]
打印多维数组
// A Java program to print 2D array using deepToString() import java.util.Arrays; public class GfG { public static void main(String[] args) { // Create a 2D array int[][] mat = new int[2][2]; mat[0][0] = 99; mat[0][1] = 151; mat[1][0] = 30; mat[1][1] = 5; // print 2D integer array using deepToString() System.out.println(Arrays.deepToString(mat)); } }
输出:
[[99, 151], [30, 5]]
toString()和deepToString()
toString()适用于一维数组,但不适用于多维数组。
// Java program to demonstrate that toString works if we // want to print single dimensional array, but doesn't work // for multidimensional array. import java.util.Arrays; public class Deeptostring { public static void main(String[] args) { // Trying to print array of strings using toString String[] strs = new String[] {"practice.geeksforgeeks.org", "quiz.geeksforgeeks.org" }; System.out.println(Arrays.toString(strs)); // Trying to print multidimensional array using // toString int[][] mat = new int[2][2]; mat[0][0] = 99; mat[0][1] = 151; mat[1][0] = 30; mat[1][1] = 50; System.out.println(Arrays.toString(mat)); } }
输出:
[practice.geeksforgeeks.org, quiz.geeksforgeeks.org] [[I@15db9742, [I@6d06d69c]
注意:我们可以使用deepToString()使用循环来打印多维数组的内容。
deepToString()适用于一维和多维,但不适用于基元的一维数组
// Java program to demonstrate that deepToString(strs)) // works for single dimensional arrays also, but doesn't // work single dimensional array of primitive types. import java.util.Arrays; public class Deeptostring { public static void main(String[] args) { String[] strs = new String[] {"practice.geeksforgeeks.org", "quiz.geeksforgeeks.org" }; System.out.println(Arrays.deepToString(strs)); Integer [] arr1 = {10, 20, 30, 40}; System.out.println(Arrays.deepToString(arr1)); /* Uncommenting below code would cause error as deepToString() doesn't work for primitive types int [] arr2 = {10, 20, 30, 40}; System.out.println(Arrays.deepToString(arr2)); */ } }
输出:
[practice.geeksforgeeks.org, quiz.geeksforgeeks.org] [10, 20, 30, 40]
参考:https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#deepToString(java.lang.Object[])
这篇关于Java Arrays.deepToString()用法及代码示例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-23线下车企门店如何实现线上线下融合?
- 2024-12-23鸿蒙Next ArkTS编程规范总结
- 2024-12-23物流团队冬至高效运转,哪款办公软件可助力风险评估?
- 2024-12-23优化库存,提升效率:医药企业如何借助看板软件实现仓库智能化
- 2024-12-23项目管理零负担!轻量化看板工具如何助力团队协作
- 2024-12-23电商活动复盘,为何是团队成长的核心环节?
- 2024-12-23鸿蒙Next ArkTS高性能编程实战
- 2024-12-23数据驱动:电商复盘从基础到进阶!
- 2024-12-23从数据到客户:跨境电商如何通过销售跟踪工具提升营销精准度?
- 2024-12-23汽车4S店运营效率提升的核心工具