“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
First, the concept of arrays
A collection of data of the same type. An array is just a container. There’s a lot of data involved in the operation, so what’s the first thing you need to do. Rather than how to operation how to save the data so that the later operation, then an array is a way to store the data, where you can save data we call it the container, the container is filled with an array of elements, can hold any type of data array, although you can install any type of data, but can only be defined array with one element, That is, once an array is defined, the type of data stored in it is determined. What’s the difference between saving data and not saving data? The great thing about arrays is that they automatically number all the elements they store. Note that the numbers start at 0. Easy to manipulate the data.
Declare an array
- A collection of data of the same type (which can be classes);
2) An array is an object;
3) Declare an array without creating an object;
4) Arrays can be declared in the following form:
Int [] I or int I []
Car[] c or Car c[]
* Only Car c in C++ []
[] c;
Create an array
- Int [] I = new int[2];
Car[] c = new Car[100];
3) The array has an initial value after creation.
The numeric type is 0, the Boolean type is false, and the reference type is NULL
Initialize the array
- Initialize, create, and separate declarations
int[] i;
i = new int[2];
i[0] = 0;
i[1] = 1;
Copy the code
2) Initialize, create, and declare at the same time
Int [] I = {0, 1}; Car[] c = {new Car(),new Car()};Copy the code
Multidimensional arrays
- effective
int[][] i1 = new int[2][3];
int[][] i2 = new int[2][];
i2[0] = new int[2],i2[1] = new int[3];
Copy the code
*C++ int[][] =new int[][3]; effective
2) is invalid
int[][] i1 = new int[][3];
Copy the code
3) Array length ———— Array property length
int[] i = new int[5]; int len = i.length; //len = 5; Student[][] st = new Student[4][6]; len = st.length; //len = 4; len = st[0].length; //len = 6;Copy the code
Array copy
System.arrayCopy(Object src, int srcPos, Object dest, int destPos, int length);
Copy the code
Copy an array to another array.