array
1. Definition of array
- An array is an ordered collection of data of the same type
- Array describes several data of the same type, which are arranged and combined in a certain order
- Among them, each data is called an array element, and each array element can access them through a subscript
2. Array declaration creation
- Array variables must be declared before arrays can be used in programs The following is the syntax for declaring array variables:
dataType [] arrayName;//Preferred method or dataType arrayName [];//The effect is the same, but it is not recommended
- The Java language uses the new operator to create arrays. The syntax is as follows:
dataType [] arraName = new dataType[arraySize];
- The elements of the array are accessed through the index. The array index starts from 0
- Get array length:
arrays.length
3. Initialization of array
1. Static initialization
int[] a = {1, 2, 3}; Man [] mans={ new Man(1,1),new Man(2,2)};
2. Dynamic initialization
int[] a = new int[2]; a[0]=1; a[1]=2;
3. Default initialization of array
- Array is a reference type. 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
package arrays array; public class Initialize array { public static void main(String[] args) { //Static initialization: create + assign int[] a = {1, 2, 3, 4, 5}; System.out.println(a[0]); //Dynamic initialization: including default initialization int[] b = new int[10]; String[] s = new String[5]; b[0] = 10; b[1] = 10; System.out.println(b[0]); System.out.println(b[1]); System.out.println(b[2]); System.out.println(b[3]); System.out.println(s[1]); System.out.println(s[4]); } } /* 1 10 10 0 0 null null */
4. Four basic characteristics of array
- Its length is definite Once an array is created, its size cannot be changed
- Its elements must be of the same type, and mixed types are not allowed
- The elements in the array can be any data type, including basic type and reference type
- Array variable belongs to reference type. Array can be regarded as an object. Each element in the array is equivalent to the member variable of the object
- The array itself is an object. In Java, the object is in the heap. Therefore, whether the array saves the original type or other object types, the array object itself is in the heap
5. Array boundary
6. Use of arrays
for loop
package arrays array; public class ArrayDemo { public static void main(String[] args) { int[] arrays = {1, 2, 3, 4, 5}; //Print all array elements for (int i = 0; i < arrays.length; i++) { System.out.println(arrays[i]); } System.out.println("-----------------"); //Calculate the sum of all elements int sum = 0; for (int i = 0; i < arrays.length; i++) { sum += arrays[i]; } System.out.println("sum=" + sum); System.out.println("---------------------"); //Find maximum element int max = arrays[0]; for (int i = 0; i < arrays.length; i++) { if (arrays[i] > arrays[0]) { max = arrays[i]; } } System.out.println("max=" + max); } } /* 1 2 3 4 5 ----------------- sum=15 --------------------- max=5 */
foreach enhanced for loop array as method input parameter array as return value
package arrays array; public class ForEach { public static void main(String[] args) { int[] arrays = {1, 2, 3, 4, 5}; // //JDK1.5 No subscript // for (int array : arrays) { // System.out.println(array); // } // printArray(arrays); int[] reverse = reverse(arrays); printArray(reverse); } //Print array elements public static void printArray(int[] arrays) { for (int i = 0; i < arrays.length; i++) { System.out.println(arrays[i] + " "); } } //Invert array public static int[] reverse(int[] arrays) { int[] result = new int[arrays.length]; //Reverse operation for (int i = 0, j = result.length - 1; i < arrays.length; i++, j--) { result[j] = arrays[i]; } return result; } }
7. Multidimensional array
Two dimensional array
- Multidimensional array can be regarded as an array of arrays. For example, two-dimensional array is a special one-dimensional array, and each element is a one-dimensional array
- Two dimensional array
int a [][] = new int[2][5];
- Analysis: the above two-dimensional array a can be regarded as an array with two rows and five columns
- Thinking: the use of multidimensional arrays?
package arrays array; public class Two dimensional array { public static void main(String[] args) { //[4] [2] four rows and two columns /* 1,2 array[0] 2,3 array[1] 3,4 array[2] 4,5 array[3] */ int[][] array = {{1, 2}, {2, 3}, {3, 4}, {4, 5}}; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.println(array[i][j]); } } System.out.println("--------------------"); System.out.println(array[0][0]); System.out.println(array[0][1]); System.out.println(array[2][0]); System.out.println(array[2][1]); System.out.println("---------------"); printArray(array[0]); printArray(array[1]); System.out.println("---------------"); System.out.println(array.length); System.out.println(array[0].length); } //Print array elements public static void printArray(int[] arrays) { for (int i = 0; i < arrays.length; i++) { System.out.println(arrays[i] + " "); } } } /* 1 2 2 3 3 4 4 5 -------------------- 1 2 3 4 --------------- 1 2 2 3 --------------- 4 2 */
8. Arrays class
- Array tool class java util. Arrays
- There are no methods for us to call array objects, but the API provides a tool class Arrays for us to use, so we can perform some basic operations on data objects
- View JDK help documentation
- The methods in the Arrays class are static methods decorated with static. When using them, you can call them directly with the class name instead of using the object (Note: it is "no" instead of "no")
- It has the following common functions:
- Assign a value to the array: through the fill method
- Sort array: sort by default
- Compare arrays: use the equals method to compare whether the element values in the array are equal
- Search array elements: binary search can be used to search the sorted array
package arrays array; import java.util.Arrays; public class ArraysDemo { public static void main(String[] args) { int[] a = {14, 345, 456, 34, 234}; System.out.println(a); //Print array element arrays toString System.out.println(Arrays.toString(a));//[I@1b6d3586 printArray(a); System.out.println(); Arrays.sort(a); System.out.println(Arrays.toString(a)); } public static void printArray(int[] a) { for (int i = 0; i < a.length; i++) { if (i == 0) { System.out.print("["); } if (i == a.length - 1) { System.out.print(a[i] + "]"); } else { System.out.print(a[i] + ", "); } } } }
9. Bubble sorting
- Bubble sorting is undoubtedly one of the most famous sorting algorithms. There are eight sorting algorithms in total!
- The bubbling code is quite simple. The two-layer cycle, the number of bubbling rounds in the outer layer and the inner layer are compared in turn
- Thinking: how to optimize?
package arrays array; import java.util.Arrays; //Bubble sorting /* 1.Compare two adjacent elements in the array. If the first number is larger than the second number, exchange positions 2.Each comparison will produce a maximum or minimum number 3.The next round can be sorted less once 4.Cycle successively until the end */ public class Bubbling { public static void main(String[] args) { int[] a = {1, 34, 23, 2, 3, 3, 3, 23, 45, 6, 3}; int[] sort = sort(a); System.out.println(Arrays.toString(sort)); } public static int[] sort(int[] array) { //Temporary variable temp temporary employee int temp = 0; //Outer circle, judge how many times we have to go for (int i = 0; i < array.length - 1; i++) { //Optimized flag boolean flag = false;//The flag flag flag bit is used to reduce meaningless comparison //The inner loop compares and judges two numbers. If the first number is larger than the second number, the position is exchanged for (int j = 0; j < array.length - 1 - i; j++) { if (array[j + 1] < array[j]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; flag = true; } } if (flag == false) { break; } } return array; } }
10. Sparse array
expand
Memory analysis