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 create, initialize, and get elements for Java two-dimensional arrays

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the "Java two-dimensional array how to create, initialize and obtain elements" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "Java two-dimensional array how to create, initialize and obtain elements" article can help you solve the problem.

Creation of one-dimensional and two-dimensional arrays

In fact, two-dimensional arrays are not supported in Java, but a feature of array declaration is that it can be any data declaration of the type of elements in the array. Therefore, a two-dimensional array can be regarded as an one-dimensional array of an one-dimensional array. The specific declaration format is as follows:

Type [] [] arr; / / data type [] [] array name

If you think of a two-dimensional array as a table, the first bracket represents the row of the table, and the second bracket represents the column of the table.

Initialization of int [] [] arr1;char [] [] arr2; two-dimensional and two-dimensional arrays

The initialization of a two-dimensional array is very similar to that of an one-dimensional array, with three methods, as follows:

(1) given the numerical space, then assign the value.

Int [] [] num=new int [2] [2]; int [0] [0] = 1 countries [0] [1] = 2 countries [1] [0] = 3 countries [1] [1] = 4

(2) assign values to the array through new.

Int [] [] num=new int [] [] {{1,2}, {3,4}}

(3) assign values to the array directly.

Int [] [] num= {{1pm 2}, {3pm 4}}; acquisition of elements in three-dimensional and two-dimensional arrays

(1) get a single element

If the two-dimensional array creates m rows and n columns, then the subscript that can be obtained is the same as the one-dimensional array, starting with 0 and going to mmur1 or nMul bit. That is, arr [0] [0] represents the element of the first row and the first column, and arr [2] [3] represents the element of the third row and the fourth column. With this rule, we can easily get a single element in the array.

Public static void main (String [] args) {double [] [] class_score = {{10.0 args 9999}, {100Yue98 97}, {100Yue9999.5}; System.out.println ("value of the second column of the second row:" + class_score [1] [1]); System.out.println ("the value of the first column of the fourth row:" + class_score [3] [0]);}

After execution, output the result:

The value of the second column element in the second row: 98.0

The value of the first column element in the fourth row: 99.5

(2) get all elements

Gets the length of an one-dimensional array, which is usually represented by arr.length. In a two-dimensional array, arr.length represents only the length of array rows, and arr [0] .length is used to represent the length of array columns.

The easiest way to get all the elements in an array is to get each element in the array through a for loop. An one-dimensional array can get all the elements with a for loop, while a two-dimensional array needs a nested for loop.

Public static void main (String [] args) {double [] [] class_score = {{100,99,99}, {100,98,97}, {100,100,99.5}, {99.5,99,98.5}}; for (int I = 0; I < class_score.length; iota +) {/ / traversing the line for (int j = 0; j < class_ score [I] .length) ) {System.out.println ("class_score [" + I + "] [" + j + "] =" + class_ score [I] [j]);}

After execution, run the result:

Class_score [0] [0] = 100.0class_score [0] [1] = 99.0class_score [0] [2] = 99.0class_score [1] [0] = 100.0class_score [1] [1] = 98.0class_score [1] [2] [0] = 100.0class_score [2] [1] = 100.0class_score [2] [2] = 99.5class_score [3] [0] = 99.5class_score [3] [1] = 99 .0class _ score [3] [2] = 98.5 about "how to create a Java two-dimensional array, That's all for initializing and getting elements. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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