Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply Java two-dimensional array

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "Java two-dimensional array how to apply", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Java two-dimensional array how to apply" it!

1. What is a two-dimensional array

An array in which multiple elements are one-dimensional arrays in a two-dimensional array is called a two-dimensional array.

two。 Define format

Format 1:

Data type of element [] [] name of array = data type of new element [length of two-dimensional array] [length of one-dimensional array]

Int [] [] a = new int [3] [2]

Explanation: there are also three one-dimensional arrays in this two-dimensional array, each with two elements.

Format 2:

Data type of element [] [] name of array = data type of new element [length of two-dimensional array] []

Int [] [] a = new int [3] []

Explanation: there are also three one-dimensional arrays in this two-dimensional array, and the number of elements in each one-dimensional array is uncertain.

Format 3:

The data type of the element [] [] array name = {{element 1, element 2, … }, {element 1, element 2, … }, }

Int [] [] a = {{1rec 2jue 3}, {4je 5}, {5pr 6pr 7je 8}}

Explanation: there are three one-dimensional arrays in this two-dimensional array, and the number of elements in each one-dimensional array is different.

The first one-dimensional array contains three elements.

The second one-dimensional array contains two elements.

The third one-dimensional array contains four elements.

3. The use of two-dimensional arrays package shuzu;public class erwei {public static void main (String [] args) {int [] [] a = new int [3] []; System.out.println (a); / [[I@7852e922, the address of the output array a / / because each array is not initialized, the three arrays are null. System.out.println (a [0]); / / null System.out.println (a [1]); / / null System.out.println (a [2]); / / null int [] aqum1 = {1pm 2pm 3}; / / define an one-dimensional array of a1. A [0] = aql; / / assign the value of the array aqum1 to the first one-dimensional array of array a. Int [] aqum2 = {4pm 5}; / / defines an one-dimensional array of a2. A [1] = aq2; / / assign the value of the array aqum2 to the second one-dimensional array of array a. Int [] aqum3 = {6 I@70dea4e 7, 8, 9}; a [2] = axi3; System.out.println (a [0]); / / [I@4e25154f, output the address of the first one-dimensional array System.out.println (a [1]); / / [I@70dea4e, output the address of the second one-dimensional array System.out.println (a [2]) / / [I@5c647e05, output the address of the third one-dimensional array / / the following program is the element of the output array: System.out.println (a [0] [0]); / / 1 outputs the first element of the first array, System.out.println (a [0] [1]) / / 2 outputs the second element of the first array System.out.println (a [0] [2]); / 3 outputs the third element of the first array System.out.println (a [1] [0]); / 4 similarly outputs the first element of the second array System.out.println (a [1] [1]) / / 5 System.out.println (a [2] [0]); / 6 similarly outputs the first element of the third array, System.out.println (a [2] [1]); / 7 System.out.println (a [2] [2]); / 8 System.out.println (a [2] [3]) / / 9}} 4. Traversal of two-dimensional array

The for each loop statement cannot automatically process every element in a two-dimensional array because it processes rows, and each one-dimensional array is a row, so two nested loops are used to loop each element of the two-dimensional array, as shown below:

Example 1:

/ / use the format: for (double [] row: a) for (double value: row) do something with value// example: double [] [] arr = {{1 row 2 5 row 4}, {4 2 row 7}, {3 row 6 row 9 4}}; how many elements are there in the for (double [] row: arr) / / cycle, which refers to an one-dimensional array. {how many elements are there in the for (double b: row) / / loop, where elements refer to the elements in each one-dimensional array. System.out.print (b + "); / / prints out the elements of each array. System.out.println (); / / Line break}

/ / output result:

1.0 2.0 5.0 4.0

4.0 2.0 5.0 7.0

3.0 6.0 9.0 4.0

Example 2:

Int [] [] a = {{1Power2, 3}, {4, 5}, {5, 6, 7, 7, 8}}; for (int I = 0; I < a. Resume; iDiffect +) / / this structure principle is roughly the same as the above, except that there are restrictions on the subscript of the element. {for (int j = 0; j < a [I] .length; jacks +) / / elements of the loop I array. System.out.print (a [I] [j] + "); / / outputs the j element of the I array. System.out.println (); / / line wrapping. }

/ / output result:

1 2 3

4 5

5 6 7 8

Thank you for your reading, the above is the content of "how to apply Java two-dimensional array". After the study of this article, I believe you have a deeper understanding of how to apply Java two-dimensional array, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report