One 、 brief introduction
describe :
- An array is The same type of data Of Orderly aggregate .
- among , Each piece of data is called an array Elements , Each array element can pass through a Subscript To visit them .
Basic characteristics :
- The length of the array is determined . Once the array is created , Its size can't be changed .
- Array Elements must be of the same type , Mixing type... Is not allowed .
- Array The element can be any data type , Including basic types and reference types .
- Array variables are of reference type , Arrays can also be thought of as objects , Each element in the array is equivalent to the member variable of the object . An array itself is an object ,Java The object in the middle is in the heap , So arrays hold either basic data types or other object types , The array object itself is in the heap .
Two 、 Declaration array
grammar :
/* The first one is , Bracket after data type */elementType[] arrayVarName/* The second kind , Bracket after variable name */elementType arrayVarName[]
describe :
- First, you must declare variables of array type , In order to freely use array objects in the program .
- Java There are two ways to declare array variables in , A kind of The brackets are in ' data type ' after , Another kind of bracket is in ' Variable name ' after .
Be careful :
- When declaring array variables , At the beginning ' data type '( Without brackets ) Represents the array of Elements Data type of .
- Declare the second type of array variable The brackets are in ' Variable name ' After the way , come from C/C++ The style of language , It's for convenience C/C++ Programmers are learning Java When you can get started faster , Not recommended .
- When an array is declared, it is actually created , No objects are instantiated .
- The length of an array cannot be specified when it is declared ( The number of elements in the array ), Only when you create an array object ,JVM To allocate space , Now it's about length .
Example :
/* Recommended */int[] intArr1;/* Not recommended */int intArr2[];
3、 ... and 、 Create array
grammar :
dynamic initialization :
new elementType[arrayLength]
initiate static :
{firstElementValue, secondElementValue, thirdElementValue...}
describe :
- There are two ways to create an array : dynamic initialization and initiate static .
- When you create an array , You must specify the length of the array , Dynamic initialization is specified in brackets , In static initialization, the array length is the number of elements in braces .
- Use dynamic initialization When creating an array object , The length of the array will be specified first , And initialize the element by default .
- Use initiate static When creating an array object , Not only will the array length be specified and the elements will be initialized by default , It also assigns values to all elements .
Be careful :
An array is a reference type , Its elements are equivalent to instance variables of a class , therefore Once an array is allocated space , Each of them Elements It's also true
Example variables are the same way that Implicit initialization .
Example :
/* dynamic initialization */new int[5];/* initiate static */{1, 2, 3, 4, 5};
Four 、 Using arrays
Ⅰ、 Declare and create
grammar :
/* Declare array variables and dynamically initialize */elementType[] arrayVarName = new elementType[arrayLength];/* Declare array variables and statically initialize */elementType[] arrayVarName = {firstElementValue, secondElementValue, thirdElementValue...};
describe :
- The declaration and creation of array variables can be done in one statement , We usually operate on an array object many times in this way .
Example :
/* Declare array variables and dynamically initialize */int[] intArr1 = new int[5];/* Declare array variables and statically initialize */int[] intArr2 = {1, 2, 3, 4, 5};
Ⅱ、 The length of the array
grammar :
arrayObject.length
describe :
- The length of the array Sure