Java輸出數組的內容

來源:互聯網
上載者:User
Java輸出數組的內容

作者:chszs,轉載需註明。
作者部落客頁:http://blog.csdn.net/chszs

1. 輸出內容最常見的方式
// List<String>類型的列表List<String> list = new ArrayList<String>();list.add("First");list.add("Second");list.add("Third");list.add("Fourth");System.out.println(list);

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

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

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

2. 使用Array類輸出數組內容
// String數組String[] array = new String[] { "First", "Second", "Third", "Fourth" };System.out.println(Arrays.toString(array));

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

3. 輸出數組的數組

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

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]]

再看下面的完整例子:

import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class PrintArray {public static void main(String[] args) {List<String> list = new ArrayList<String>();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]]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.