Home  >  Article  >  Java  >  How to assign values ​​to a two-dimensional array in java

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

WBOY
WBOYforward
2023-05-05 15:25:063387browse

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("!");

( 2) Output

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

result

Good Luck to you !

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete