Java output array content

Source: Internet
Author: User

Java output array content
Author: chszs, reprinted with note.
 

1. The most common way to output content
[Java]
// List <String> type List
List <String> list = new ArrayList <String> ();
List. add ("First ");
List. add ("Second ");
List. add ("Third ");
List. add ("Fourth ");
System. out. println (list );

Output:
[First, Second, Third, Fourth]

[Java]
// String Array
<Pre name = "code" class = "java"> String [] array = new String [] {"First", "Second", "Third", "Fourth "};
System. out. println (array. toString (); </pre>

Output:
[Ljava. lang. String; @ 12dacd1

2. Use the Array class to output the Array content
[Java]
// String Array
String [] array = new String [] {"First", "Second", "Third", "Fourth "};
System. out. println (Arrays. toString (array ));

Output: www.2cto.com
[First, Second, Third, Fourth]

3. Output Array
You can use the Arrays. deepToString () method.

[Java]
String [] arr1 = new String [] {"th", "Sixth "};
String [] arr2 = new String [] {"Seventh", "Eight "};
// Array
String [] [] arrayOfArray = new String [] [] {arr1, arr2 };
// Compare the following output <br>
System. out. println (arrayOfArray); <br>
System. out. println (Arrays. toString (arrayOfArray); <br>
System. out. println (Arrays. deepToString (arrayOfArray); <br>

Output:
[[Ljava. lang. String; @ 1ad086a
[[Ljava. lang. String; @ 10385c1, [Ljava. lang. String; @ 42719c]
[[Th, Sixth], [Seventh, Eighth]

Let's look at the complete example below:
[Java]
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 [] {"th", "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 ));
}
}

Output:

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


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.