笔记:java输出数组,集合的内容


Java输出数组的内容

(2012-11-25 23:49:54)








标签:

it

分类: 软件开发

作者:chszs,转载需注明。
作者博客主页:http://blog.csdn.net/chszs


1. 输出内容最常见的方式




[java] view plain copy print ?


  1. //List类型的列表
  2. Listlist=new ArrayList();

  3. list.add("First");
  4. list.add("Second");
  5. list.add("Third");
  6. list.add("Fourth");
  7. System.out.println(list);

// List类型的列表 List list = new ArrayList(); list.add("First"); list.add("Second"); list.add("Third"); list.add("Fourth"); System.out.println(list);


输出:
[First,
Second, Third, Fourth]




[java] view plain copy print ?


  1. //String数组
  2. class=javaname="code">String[]array=new String[]{"First","Second","Third","Fourth" };

  3. System.out.println(array.toString());

// String数组




[java] view plain copy print ?


  1. String[]array=new String[]{"First","Second","Third","Fourth" };

  2. System.out.println(array.toString());

String[] array = new String[] { "First", "Second", "Third", "Fourth" }; System.out.println(array.toString());


输出:
[Ljava.lang.String;@12dacd1


2. 使用Array类输出数组内容




[java] view plain copy print ?


  1. //String数组
  2. String[]array=new String[]{"First","Second","Third","Fourth" };

  3. System.out.println(Arrays.toString(array));

// String数组 String[] array = new String[] { "First", "Second", "Third", "Fourth" }; System.out.println(Arrays.toString(array));


输出:
[First,
Second, Third, Fourth]


3. 输出数组的数组

可以使用Arrays.deepToString()方法。




[java] view plain copy print ?


  1. String[]arr1=new String[]{"Fifth","Sixth" };

  2. String[]arr2=new String[]{"Seventh","Eight" };

  3. //数组的数组
  4. String[][]arrayOfArray=new String[][]{arr1,arr2};

  5. //比较下面的输出

  6. System.out.println(arrayOfArray);

  7. System.out.println(Arrays.toString(arrayOfArray));


  8. System.out.println(Arrays.deepToString(arrayOfArray));


String[] arr1 = new String[] { "Fifth", "Sixth" }; String[] arr2 = new String[] { "Seventh", "Eight" }; // 数组的数组 String[][] arrayOfArray = new String[][] { arr1, arr2 }; // 比较下面的输出
System.out.println(arrayOfArray);
System.out.println(Arrays.toString(arrayOfArray));
System.out.println(Arrays.deepToString(arrayOfArray));


输出:
[[Ljava.lang.String;@1ad086a
[[Ljava.lang.String;@10385c1,
[Ljava.lang.String;@42719c]
[[Fifth, Sixth], [Seventh,
Eighth]]

再看下面的完整例子:



[java] view plain copy print ?


  1. import java.util.ArrayList;

  2. import java.util.Arrays;
  3. import java.util.List;


  4. public class PrintArray{

  5. public static void main(String[]args){

  6. Listlist=new ArrayList();

  7. list.add("First");

  8. list.add("Second");

  9. list.add("Third");

  10. list.add("Fourth");



  11. System.out.println(list);



  12. String[]array=new String[]{"First","Second","Third","Fourth" };

  13. System.out.println(array.toString());

  14. System.out.println(Arrays.toString(array));



  15. String[]arr1=new String[]{"Fifth","Sixth" };

  16. String[]arr2=new String[]{"Seventh","Eigth" };

  17. String[][]arrayOfArray=new String[][]{arr1,arr2};



  18. System.out.println(arrayOfArray);

  19. System.out.println(Arrays.toString(arrayOfArray));

  20. System.out.println(Arrays.deepToString(arrayOfArray));

  21. }
  22. }

import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class PrintArray { public static void main(String[] args) { List list = new ArrayList(); list.add("First"); list.add("Second"); list.add("Third"); list.add("Fourth"); System.out.println(list); String[] array = new String[] { "First", "Second", "Third", "Fourth" }; System.out.println(array.toString()); System.out.println(Arrays.toString(array)); String[] arr1 = new String[] { "Fifth", "Sixth" }; String[] arr2 = new String[] { "Seventh", "Eigth" }; String[][] arrayOfArray = new String[][] { arr1, arr2 }; System.out.println(arrayOfArray); System.out.println(Arrays.toString(arrayOfArray)); System.out.println(Arrays.deepToString(arrayOfArray)); } }


输出:

[First,
Second, Third, Fourth]
[Ljava.lang.String;@12dacd1
[First, Second, Third,
Fourth]
[[Ljava.lang.String;@1ad086a
[[Ljava.lang.String;@10385c1,
[Ljava.lang.String;@42719c]
[[Fifth, Sixth], [Seventh, Eigth]]


分享: 分享到新浪Qing




0


喜欢

你可能感兴趣的:(笔记:java输出数组,集合的内容)