Home > Java > javaTutorial > body text

How to use java two-dimensional array

(*-*)浩
Release: 2020-09-18 13:57:51
Original
8036 people have browsed it

Java's two-dimensional array is actually a nested one-bit array (each row is regarded as an inner one-dimensional array)

How to use java two-dimensional array

Definition format

数据类型[][] 数组名 = new 数据类型[二维数组的长度/包含的一维数组的个数][每个一维数组的长度];
int[][] arr = new int[3][5];//定义了一个整型的二维数组,其中包含3个一维数组,每个一维数组可以存储5个整数
arr[0]//下标为0的位置上的一维数组
arr[1][3]//如果要获取具体的元素需要两个下标
数据类型[][] 数组名 = new 数据类型[二维数组的长度/包含的一维数组的个数][];
数据类型[][] 数组名 = {{元素},{元素1,元素2},……};
int[][] arr = {{2,5},{1},{3,2,4},{1,7,5,9}};
[]在变量名前的时候,是紧跟数据类型的;如果[]在后,则是属于当前变量名。
Copy after login

int[][] arr = new int[3][];----Represents a two-dimensional array containing three one-dimensional arrays of integers

How to use java two-dimensional array

Application of two-dimensional array

The length of the two-dimensional array: array name.length ---Each one-dimensional array: array name [subscript ].length

Traversal of two-dimensional array---double for loop

for(int i = 0; i <p><strong>Enter the number of lines from the console and print the corresponding Yang Hui triangle</strong></p><pre class="brush:php;toolbar:false"> //从控制台获取行数
Scanner s = new Scanner(System.in);
int row = s.nextInt();
//根据行数定义好二维数组,由于每一行的元素个数不同,所以不定义每一行的个数
int[][] arr = new int[row][];
//遍历二维数组
for(int i = 0; i <p> Result: </p><pre class="brush:php;toolbar:false">1
1   1
1   2   1
1   3   3   1
1   4   6   4   1
1   5   10  10  5   1
Copy after login

Related learning recommendations: java basic tutorial

The above is the detailed content of How to use java two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!