About two-dimensional array __java in Java

Source: Internet
Author: User

About two-dimensional arrays in Java [instance]:

Public class lesson{public static void Main (String [] args) {//two-D array declaration://data type [] [] Array name = new data type [length] [length];//Data class Type [[] Array name = {{123},{456}}; /* int [] [] arr = {{123},{456}}; Defines a two-dimensional array of two rows of three columns and assigns a for (int x = 0; x<arr.length; x + +) {//positioned row for (int y = 0; y<arr[x].length; y++) {//Locate the number of elements per row System. Out.print (Arr[x][y]); } System.out.println ("n"); } */int [] num = new int [3][3]; Two-dimensional array num[0][0 with three rows and three columns defined] = 1; Assign a value to the first element of the first line num[0][1] = 2; Assign a value to the second element in the first row num[0][2] = 3; Assign a value to the third element in the first row num[1][0] = 4; Assign a value to the first element of the second line num[1][1] = 5; Assign the second element to the second row num[1][2] = 6; Assign a value to the third element of the second line num[2][0] = 7; Assign a value to the first element of the third line num[2][1] = 8; Assign a value to the second element of the third row num[2][2] = 9; Assign a third row to the third element for (int x = 0; x<num.length; x + +) {//Locate line for (int y = 0; y<num[x].length; y++) {//Locate the number of elements per row System.out. Print (Num[x][y]); } System.out.println ("n"); }}//array value Arr[x][y] represents the value specified in column y of line X. When using a two-dimensional array object, note the length represented by length, and/or the array name, followed by a direct length (such as Arr.length), which refers to a few rows (row);//Specify the index followed by length (such as Arr[0].length), Refers to the element that the row owns, that is, the number of columns (column).

Related Article

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.