Java two-dimensional array learning

Source: Internet
Author: User

Turn from: http://blog.csdn.net/java1992/article/details/5808709, I thanked you here.


/*
* Java Learning:
* Use of two-dimensional arrays :
*/
public class Test {

public static void Main (string[] args) {
How to declare a two-dimensional array:
data type [] Array name = new data type [length] [length];
data type [] Array name = {{123},{456}};

/*
int [] arr = {{123},{456}}; Defines two-dimensional arrays of three columns and assigns values
for (int x = 0; x<arr.length; x + +) {//Anchor Line
for (int y = 0; y<arr[x].length; y++) {//position the number of elements per row
System.out.print (Arr[x][y]);
}
System.out.println ("\ n");
}
*/
int [] [] num = new int [3][3]; Defines a two-dimensional array of three rows and three columns
Num[0][0] = 1; Assign a value to the first element of the first row
NUM[0][1] = 2; Assign a value to the second element of the first row
NUM[0][2] = 3; Assign a value to the third element of the first row

NUM[1][0] = 4; Assign a value to the first element in the second row
NUM[1][1] = 5; Assign a value to the second element of the second row
NUM[1][2] = 6; Assign a value to the third element of the second row

Num[2][0] = 7; Assign a value to the first element in the third row
NUM[2][1] = 8; Assign a value to the second element of the third row
NUM[2][2] = 9; Assign a value to the third element of the third row
for (int x = 0; x<num.length; x + +) {//Anchor Line
for (int y = 0; y<num[x].length; y++) {//position the number of elements per row
System.out.print (Num[x][y]);
}
System.out.println ("\ n");
}
}
}
Array value Arr[x][y] indicates that the value specified is the y column of row x.
When using a two-dimensional array object, note the length represented by
The array name is followed by a length (such as Arr.length), which refers to a few rows (row);
Specifies the index with length (such as Arr[0].length), which is the number of elements owned by the row, that is, the column.

Java two-dimensional array learning (GO)

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.