Home > Java > javaTutorial > body text

How to assign values ​​to a two-dimensional array in java

WBOY
Release: 2023-05-05 15:25:06
forward
3396 people have browsed it

1. Assignment concept

Use double subscripts to access elements in a two-dimensional array:

The first subscript represents: row number (under high dimensions mark).

The second subscript represents: column number (low-dimensional subscript).

2. Assignment examples

(1) Assignment: Starting from the highest dimension, allocate space for each dimension, for example:

String s[][] = new String[2][];
s[0] = new String[2];
s[1] = new String[3];
s[0][0] = new String("Good");
s[0][1] = new String("Luck");
s[1][0] = new String("to");
s[1][1] = new String("you");
s[1][2] = new String("!");
Copy after login

( 2) Output

for (int i=0; i<s.length;i++){
    for (String s1:s[i]){
        System.out.print(s1 +' ');
    }
}
Copy after login

result

Good Luck to you !
Copy after login

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

Related labels:
source:yisu.com
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