Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How does java define arrays and initialize them
How does java define arrays and initialize them
Definition of 1. array:

Arrays can be divided into one-dimensional arrays, two-dimensional arrays and multidimensional arrays. One-dimensional arrays can be defined in the following two ways:

Type [? ]? arr _ name

Type? arr_name[? ]; A two-dimensional array is defined as follows:

Type [? ]? [? ]? arr _ name

Type? arr_name[? ]? [? ]; A multidimensional array can specify a corresponding number of [] according to its dimensions.

int[? ]? arr 1;

String [? ]? arr2

Floating? arr3[? ]? ;

String [? ]? [? ]? Arr42. Initialization of array:

There are two ways to initialize an array.

Static initialization

Static initialization refers to specifying the contents of array elements when defining. The sample code is as follows:

int[]? arr 1? =? { 1,2,3,4,5};

String [? ]? arr2? =? {"Tom", "Ross" and "Sonny"};

String [? ][? ]? arr3? =? {? {"Tom", "American"}, {"Jack", "British"}, {"Zhang San", "China"}? }; Dynamic initialization

Dynamic initialization means that when defining, first open a storage space with a specified size through the new keyword, and then specify the contents for the array.

int[? ]? arr 1? =? New? int[2? ]? ;

arr 1[0]? =? 10;

arr2[ 1]? =? 20;

arr3[? 2]? =? 30;