JavaBasic Learning--array first knowledge (1)1what is an array
A common data structure in Java is an array, which can be divided into one-dimensional arrays, two-dimensional arrays, and multidimensional arrays.
An array is a data type that consists of a set of identical variables, each of which has the same data type, and each element in the array can be determined with a uniform array name and subscript.
2Use of Arrays
General use steps for arrays:
Declaring an array
allocating memory to the array
The following is an example of a one-dimensional array:
Data Type Array name [];
array Name = new data type [ data Count ];
2.1Declaration and assignment of one-dimensional arrays
1, the declaration of the Array
int num []; declares an array
num = new INT[5]; allocates memory space to an array of 5 elements
The declaration of an array can also take the following form:
int num []= new int [5]; allocating memory space at the same time as declared
2. Assigning values to arrays
the assigned initial value of an array is the following form:
int num []={1,2,3,4,5}; // This represents the declaration and assignment of an array, although it appears that there is no
Specifies the number of data in the array, but because the curly braces
given the specific data, so the system allocates space by default
3. The representation of the elements in thearray and the length of the array
the representation of an element in an array:
Array name [Subscript of elements in array ]//The subscript of an element in an array is starting from 0
For example, the representation of the second element in the array is
num [1]// Gets the second element of the array
How to represent the length of an array:
The array name . ength
that is, the length of the above array is num. Length
2.2two-dimensional arrays
1. Declaration of two-dimensional arrays
int num []; declaring a two-dimensional array
num = new INT[2][3]; allocates memory space to an array with elements of 2 rows and 3 columns
The declaration of an array can also take the following form:
int num [][]= new int [2][3]; allocating memory space at the same time as declared
2. Assigning values to arrays
the assigned initial value of an array is the following form:
int num []={{1,2,3},{4,5,6}}; // This represents the declaration and assignment of an array, although it appears that there is no
Specifies the number of data in the array, but because the curly braces
given the specific data, so the system allocates space by default
3.the representation of the elements in the array and the length of the array rows
the representation of an element in an array:
Array name [ row subscript] [ column subscript]//The index of the element in the array is starting from 0
For example, the second row in the array is represented by the next column element.
num [1][1]// Gets the second row of the array of elements
How to represent the length of an array:
array name . ength// Gets the number of rows in the array
Array name [ row below table ].ength// Get the number of elements of the corresponding row
This article is from the "Broken Castle of the Little Prince" blog, please be sure to keep this source http://wh201503.blog.51cto.com/10047381/1890781
Basic Java Learning--array first knowledge (1)