1. Definition of array
An array is an ordered collection of data of the same type. Array describes several data of the same type according to a certain priority
It is formed by arrangement and combination in the later order. Among them, each data is called an element, and each element can pass an index (subscript)
To access them. Four basic features of arrays:
- The length is determined. Once an array is created, its size cannot be changed.
- The type of its element must be the same type, and mixed types are not allowed.
- Array types can be any data type, including basic types and reference types.
- Array variables are of reference type, and arrays are also objects.
Old bird advice Array variables belong to the reference type. Array is also an object. Each element in the array is equivalent to the member variable of the object. number The group itself is an object, Java The objects in the array are in the heap, so the array holds both the original type and other object classes The array object itself is stored in the heap.
2. Create array and initialize
type[] arr_name; //Mode 1 (recommended) type arr_name[]; //Mode 2
matters needing attention
no object is instantiated at the time of declaration. The JVM allocates space only when the array object is instantiated, which is related to the length.
when declaring an array, no array is actually created.
to construct an array, the length must be specified.
2.1 array declaration
2.1.1 create one-dimensional array of basic types
public class Test { public static void main(String[] args) { int[ ] s = null; // Declaration array; s = new int[10]; // Allocate space to the array; for (int i = 0; i < 10; i++) { s[i] = 2 * i + 1;//Assign values to array elements; An array is an object, and the elements in the array are the attributes of the object System.out.println(s[i]); } } }
2.1.2 create a one-dimensional array of reference types
class Man{ int age; int id; public Man(int id, int age) { this.age = age; this.id = id; } } public class AppMain { public static void main(String[] args) { Man[] mans; //Declare an array of reference types; mans = new Man[10]; //Allocate space to the reference type array; Man m1 = new Man(1,11); Man m2 = new Man(2,22); mans[0]=m1;//Assign a value to the element of the reference type array; mans[1]=m2;//Assign a value to the element of the reference type array; } }
2.2 initialization
There are three initialization methods for arrays: static initialization, dynamic initialization and default initialization. These three methods are explained below.
2.2.1 static initialization
In addition to using the new keyword to generate an array, you can also directly allocate space and assign values to the array elements while defining the array.
int [] a = {1, 2, 3};// Static initialization of basic type array; Man[] mans = {new Man(1, 1), new Man(2, 2)};// Statically initialize the reference type array;
2.2.2 dynamic initialization
Array definition is separated from the operation of allocating space and assigning values to array elements.
int[] a1 = new int[2];//Dynamically initialize the array and allocate space first; a1[0]=1;//Assign values to array elements; a1[1]=2;//Assign values to array elements;
2.2.3 default initialization
Array is a reference type, and its elements are equivalent to the instance variables of the class. Therefore, once the array is allocated space, each element in it is implicitly initialized in the same way as the instance variables.
int a2[] = new int[2]; // Default: 0,0 boolean[] b = new boolean[2]; // Default value: false,false String[] s = new String[2]; // Default: null, null
3. Common operations of array
After the array is created, we often need to do two most common operations on the array: traversal and copy.
1. Traversal refers to "traversing all elements of an array through a loop".
2. Copy refers to copying the contents of an array to another array. In fact, as you will learn later, container expansion is essentially "copy of array".
3.1 traversal of array
Legal range of array element subscript: [0, length-1]. We can traverse the elements in the array through subscripts. When traversing, we can read the value of the element or modify the value of the element.
public class Test { public static void main(String[] args) { int[] a = new int[4]; //Initializes the value of an array element for(int i=0;i<a.length;i++){ a[i] = 100*i; } //Read the value of the element for(int i=0;i<a.length;i++) { System.out.println(a[i]); } } } /* Execution result: 0 100 200 300 Process finished with exit code 0 */
3.2 for each cycle
Enhanced for loop for each is jdk1 5. The newly added function is specially used to read all elements in the array or collection, that is, to traverse the array. (enhanced for loop)
public class Test { public static void main(String[] args) { int[] a = new int[4]; //Initializes the value of an array element for(int i=0;i<a.length;i++){ a[i] = 100*i; } //Read the value of the element for (int value : a) { System.out.println(value); } } }
matters needing attention
for each enhanced for loop cannot modify the value of an element in the array during traversing the array.
for each is only applicable to traversal and does not involve operations related to indexes (subscripts).
3.3 copy of array
The System class also contains a static void array copy (object src, int srcpos, object dest, int destpos, int length) method, which can assign the element value in the src array to the element of the dest array, where srcpos specifies that the assignment starts from the first element of the src array, The length parameter specifies how many elements of the src array are assigned to the elements of the dest array.
public class Test { public static void main(String[] args) { String[ ] s = {"Ali","Shang Xuetang","JD.COM","Sohu","Netease"}; String[ ] sBak = new String[6]; System.arraycopy(s,0,sBak,0,s.length); for (String value : sBak) { System.out.println(value); } } }
3.4 java.util.Arrays class
JDK provides Java util. Arrays class contains common array operations to facilitate our daily development. The arrays class contains common operations such as sorting, finding, filling, and printing content.
3.4.1 use the Arrays class to output the elements in the array
import java.util.Arrays; public class Test { public static void main(String[] args) { int[] a = {1, 2, 3, 4}; System.out.println(Arrays.toString(a)); // Print the values of array elements; } } //Arrays here The toString() method is a static method of the arrays class, not the toString() method of Object.
3.4.2 using Arrays class to sort array elements
import java.util.Arrays; public class Test { public static void main(String[] args) { int[ ] a = {1,2,323,23,543,12,59}; Arrays.sort(a); System.out.println(Arrays.toString(a)); } }
3.4.3 use the Arrays class to realize the dichotomy search method
import java.util.Arrays; public class Test { public static void main(String[] args) { int[] a = {1,2,323,23,543,12,59}; Arrays.sort(a); //When using dichotomy search, you must sort the array first; System.out.println(Arrays.toString(a)); //Returns the new index position after sorting. If it is not found, it returns a negative number. System.out.println("Index of this element:"+Arrays.binarySearch(a, 12)); } }
3.4.4 use the Arrays class to fill the array
import java.util.Arrays; public class Test { public static void main(String[] args) { int[ ] a= {1,2,323,23,543,12,59}; System.out.println(Arrays.toString(a)); Arrays.fill(a, 2, 4, 100); //Replace the elements of 2 to 4 indexes with 100; System.out.println(Arrays.toString(a)); } }