Initialization and output of arrays

Source: Internet
Author: User

A two-dimensional array can not initialize the number of columns (second dimension).

The following example shows two different ways to store a two-dimensional array and output:

1. This is our easy-to-understand two-dimensional array storage method:

string[][] data = new string[][] {{"Youth", "High", "No", "fair", "No"},{"youth", "High", "No", "excellent", "No"},{" Middle_aged "," High "," No "," fair "," Yes "},};for (string[] s:data) {for (String str:s) System.out.print (str +" \ T "); s Ystem.out.println ();}

Output Result:

Youthhighnofairnoyouthhighnoexcellentnomiddle_agedhighnofairyes

2. This is the method of defining the second dimension as an array of strings, which is required for the output to be forced character conversions:

object[] array = new object[] {new string[] {"Age", "income", "Student", "credit_rating", "Buys_computer"},new string[] { "Youth", "High", "No", "fair", "No"},new string[] {"Youth", "High", "No", "excellent", "no"},;                     for (int i = 0, i < array.length; i++) {for (int j = 0; J < ((string[]) array[0]). length; j + +) System.out.print (<s Pan style= "color: #ff0000;" > ((string[]) array[i]) [j]</span> + "\ t"); System.out.println ();}

The output is the same as above.

Initialization and output of arrays

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.