1, Array
1. One dimensional array
Concept: a container for a set of data (an array can hold multiple data)
be careful:
1. Array is a reference data type
2. The data in an array is also called an element
3. Each element has a number called subscript / index
4. Subscript starts from 0
5. After array initialization, a series of continuous spaces will be opened in memory
6. Once the array is initialized, its length cannot be changed (the array is not expanded or deleted)
7. Array operations: add, modify and query
Declaration of array: data type [] array name;
Initialization of array:
Static initialization: the data is specified by the programmer and the length is allocated by the system
Dynamic initialization: the length is specified by the programmer, and the data is allocated by the system (default)
Integer type: 0
Floating point type: 0.0
Character type: '
Boolean type: false
Reference type: null (empty)
Traversal for loop
Traversal - foreach (enhanced for loop)
When traversing, use the subscript for loop
When traversing, we use foreach instead of subscript
Static initialization: the data is specified by the programmer and the length is allocated by the system
public class Test09{ public static void main(String[] args){ //Static initialization 1 //String[] names = new String [] {"Shi Mingkong", "nainainai Aida", "Shuiye Chaoyang", "Taogu hualixiang", "Lingyuan loves Millie"}; //Static initialization 2 //String[] names; //names = new String [] {"Shi Mingkong", "Nainai Aida", "Shuiye Chaoyang", "Taogu hualixiang", "Lingyuan loves Millie"}; //Static initialization 3 String[] names = {"Shi Mingkong","Nainai Aida","Shuiye Chaoyang","Peach Valley thyme","Lingyuan loves Millie"}; //Sets the element on the specified subscript names[3] = "aa"; //Gets the element on the specified subscript String n = names[3]; System.out.println("Gets the element on the specified subscript:" + n);// //ArrayIndexOutOfBoundsException - array index out of bounds exception //System.out.println(names[100]); //Get the number of elements int len = names.length; System.out.println("Get the number of elements:" + len);//5 System.out.println("-----------"); //Traversal for loop for(int i = 0;i<names.length;i++){ System.out.println(names[i]); } System.out.println("-----------"); //Traversal - foreach (enhanced for loop) for(String str:names){//Traverse the array and assign the elements to str in turn System.out.println(str); } for(String s:names){ System.out.println(s); } } }
Dynamic initialization: the length is specified by the programmer, and the data is allocated by the system (default)
public class Test10{ public static void main(String[] args){ //Dynamic initialization 1 String[] names = new String[5];//5 - > 5 lengths //Sets the element on the specified subscript names[0] = "aa"; names[1] = "bb"; names[2] = "cc"; names[3] = "dd"; names[4] = "ee"; //Gets the element on the specified subscript String n = names[3]; System.out.println("Gets the element on the specified subscript:" + n);// //ArrayIndexOutOfBoundsException - array index out of bounds exception //System.out.println(names[100]); //Get the number of elements int len = names.length; System.out.println("Get the number of elements:" + len);//5 System.out.println("-----------"); //Traversal for loop for(int i = 0;i<names.length;i++){ System.out.println(names[i]); } System.out.println("-----------"); //Traversal - foreach (enhanced for loop) for(String str:names){//Traverse the array and assign the elements to str in turn System.out.println(str); } } }
1.1 static initialization and dynamic initialization
Static initialization vs dynamic initialization
Know the data from the beginning: static initialization
Know the length from the beginning: dynamic initialization
1.2 array sorting - bubble sorting
Pithy formula:
N numbers to sort
Compared with two, it is small and forward
Outer cycle N-1
Inner circulation N-1-i
public class Test01{ public static void main(String[] args){ int[] is = {39,77,27,20,45,62}; for(int i = 0;i<is.length-1;i++){ for(int j = 0;j<is.length-1-i;j++){ if(is[j] > is[j+1]){ int temp = is[j]; is[j] = is[j+1]; is[j+1] = temp; } } } for(int num : is){ System.out.println(num); } } }
1.3 array lookup
1.3.1 sequential search
Sequential lookup: traversal from beginning to end
public class Test02{ public static void main(String[] args){ int[] is = {39,77,27,20,45,62}; int num = 77; for(int i = 0;i<is.length;i++){ if(is[i] == num){ System.out.println("Found"); } } } }
1.3.2 binary search
Premise: sort first
import java.util.Arrays; public class Test02{ public static void main(String[] args){ int[] is = {39,77,27,20,45,62}; int num = 77; //sort Arrays.sort(is); int start = 0; int end = is.length-1; while(start <= end){ int mid = (start+end)/2; if(num >is[mid]){ start = mid+1; }else if(num < is[mid]){ end = mid-1; }else{ System.out.println("Found"); break; } } } }
1.4 array copy
The first method: new a new array, directly assign the address of the original array to the new array
Disadvantages: if you modify the original array, the data of the new array will also change
public class Test03{ public static void main(String[] args){ //Original array String[] names = {"Sakurai step","HIDA Yongmei","Amy Seth","Yoshizawa Akiho"}; //New array String[] newNames = names; //Modify original array names[0] = "Sanshang Youya"; //Traverse new array for(String name : newNames){ System.out.println(name); } } }
The second method: new a new array. Copy the data in the original array to the new array. At this time, modify the original array and the data in the new array will not change
public class Test04{ public static void main(String[] args){ //Source array String[] names = {"Sakurai step","HIDA Yongmei","Amy Seth","Yoshizawa Akiho"}; //New array String[] newNames = new String[names.length]; //Assign the data in the original array to the new array in turn for(int i = 0;i<names.length;i++){ newNames[i] = names[i]; } //Modify original array names[0] = "Sanshang Youya"; //Traverse new array for(String name : newNames){ System.out.println(name); } } }
1.5 expansion of array
public class Test05{ public static void main(String[] args){ //Original array String[] names = {"Sakurai step","HIDA Yongmei","Amy Seth","Yoshizawa Akiho"}; //New array int capacity = names.length + (names.length>>1);//New capacity: 1.5 times the length of the original array String[] newNames = new String[capacity]; //Migrate all the data of the original array to the new array for(int i = 0;i<names.length;i++){ newNames[i] = names[i]; } //Assign the address of the new array to the original array names = newNames; //Traverse the original array for(String name:names){ System.out.println(name); } } }
1.6 deletion of array
The first method: create a new array and assign data other than the data to be deleted to the new array
Disadvantages: the array was originally used to store data. After deleting elements, the length of the array becomes shorter
public class Test06{ public static void main(String[] args){ //Original array String[] names = {"Sakurai step","HIDA Yongmei","Amy Seth","Yoshizawa Akiho"}; //New array String[] newNames = new String[names.length-1]; //Migrate the data of the original array to the new array, except for the elements to be deleted (Hiromi Shenda) int index = 0;//Subscript of new array for(String name:names){ if(!name.equals("HIDA Yongmei")){ newNames[index] = name; index++; } } //Assign the address of the new array to the original array names = newNames; //Traverse the original array for(String name:names){ System.out.println(name); } } }
The second method: overwrite the following data one by one, and then assign null to the last data
public class Test07{ public static void main(String[] args){ //Original array String[] names = {"Sakurai step","HIDA Yongmei","Amy Seth","Yoshizawa Akiho"}; //Data migration for(int i = 1;i<names.length-1;i++){ names[i] = names[i+1]; } names[names.length-1] = null; //Traverse the original array for(String name:names){ System.out.println(name); } } }
1.7 array parameters and return values
public class Test08{ //Requirements: design a method, pass in the array, and return the maximum and minimum values public static void main(String[] args){ int[] is = {1,2,3,4,5,6}; int[] newArr = method(is); System.out.println("The maximum value is:" + newArr[0]); System.out.println("The minimum value is:" + newArr[1]); } public static int[] method(int[] is){ int max = is[0]; int min = is[0]; for(int i = 1;i<is.length;i++){ if(max < is[i]){ max = is[i]; } if(min > is[i]){ min = is[i]; } } return new int[]{max,min}; } }
1.8 variable parameters
public class Test09{ //Requirements: design a method to pass in five int values and sum them public static void main(String[] args){ int sum = add(1,2,3,4,5,6,7);//The argument is pushed into the array as an element System.out.println(sum); } //Is is an array public static int add(int... is){ int sum = 0; for(int num : is){ sum += num; } return sum; } //Note: variable parameters cannot be followed by other parameters //error public static void method(int a,String... ss){} }
2.Arrays tools
import java.util.Arrays; public class Test11{ /** Knowledge points: Arrays tool class Arrays: java The tool class provided for us to operate on arrays Tool class: all methods in the class are static (called directly with the class name) API: java Operating instructions for class */ public static void main(String[] args){ int[] is = {39,77,27,20,45,62}; //Sort: 20 27 39 45 62 77 Arrays.sort(is); //Find (return value: if the element is in the array, return the subscript, otherwise return - insertion point - 1) int index = Arrays.binarySearch(is,42); System.out.println("The index of the lookup element is:" + index); //replace Arrays.fill(is,888); Arrays.fill(is,1,4,666); //Copy int[] copyOf = Arrays.copyOf(is,is.length*2); int[] copyOfRange = Arrays.copyOfRange(is,3,8); //Gets the string representation of the array (converts the array to a string) System.out.println(Arrays.toString(copyOfRange)); } }
3. Two dimensional array
Meaning: contains multiple one-dimensional arrays
Declaration: data type [] [] array name;
Initialization of array:
Static initialization: the data is specified by the programmer, and the length is automatically allocated by the system
public class Test01 { public static void main(String[] args) { //Static initialization 1 //String[][] names = new String [] [] {{"Akita Yongmei", "Nainai Aida", "Jingxiang julia"}, {"tomorrow's beautiful flowers", "Xiaofeng Youyi", "Mizuko zemura", "Maria Ozawa"}}; //Static initialization 2 //String[][] names; //names = new String [] []{{"Hirota Yongmei", "OTA Nainai", "Jingxiang julia"}, {"tomorrow's flowers are beautiful", "Xiaofeng Youyi", "Mizuko zemura", "Maria Ozawa"}}; //Static initialization 3 String[][] names = {{"HIDA Yongmei","Nainai Aida","Jingxiang julia"},{"Kirara Asuka ","Xiaofeng Youyi","Mizuko Nakamura","Maria Ozawa"}}; //Sets the element on the specified subscript names[0][2] = "Youya on the mountain"; //Gets the element on the specified subscript String name = names[0][2]; System.out.println("Gets the element on the specified subscript:" + name); //Get length System.out.println("Get the number of one-dimensional arrays in the two-dimensional array:" + names.length);//2 System.out.println("Get the number of elements of the first one-dimensional array in the two-dimensional array:" + names[0].length);//3 System.out.println("Get the number of elements of the second one-dimensional array in the two-dimensional array:" + names[1].length);//4 System.out.println("------------"); //Traversal for loop for (int i = 0; i < names.length; i++) { for (int j = 0; j < names[i].length; j++) { System.out.println(names[i][j]); } } System.out.println("------------"); //Traversal foreach for (String[] strings : names) { for (String string : strings) { System.out.println(string); } } } }
Dynamic initialization: the length is specified by the programmer, and the data is assigned a default value by the system
public class Test02 { public static void main(String[] args) { //Dynamic initialization 1 //String[][] names = new String[2][3];//2 one-dimensional arrays with 3 elements in each one-dimensional array //Dynamic initialization 2 String[][] names; names = new String[2][3];//2 one-dimensional arrays with 3 elements in each one-dimensional array //Sets the element on the specified subscript names[0][0] = "aa"; names[0][1] = "bb"; names[0][2] = "cc"; names[1][0] = "dd"; names[1][1] = "ee"; names[1][2] = "ff"; //Gets the element on the specified subscript String name = names[0][2]; System.out.println("Gets the element on the specified subscript:" + name); //Get length System.out.println("Get the number of one-dimensional arrays in the two-dimensional array:" + names.length);//2 System.out.println("Get the number of elements of the first one-dimensional array in the two-dimensional array:" + names[0].length);//3 System.out.println("Get the number of elements of the second one-dimensional array in the two-dimensional array:" + names[1].length);//4 System.out.println("------------"); //Traversal for loop for (int i = 0; i < names.length; i++) { for (int j = 0; j < names[i].length; j++) { System.out.println(names[i][j]); } } System.out.println("------------"); //Traversal foreach for (String[] strings : names) { for (String string : strings) { System.out.println(string); } } } }
Requirements: dynamically initialize the two-dimensional array. The first one-dimensional array has 3 elements and the second one-dimensional array has 4 elements
public class Test03 { public static void main(String[] args) { //Dynamic initialization: there are two one-dimensional arrays in the dimension two-dimensional array String[][] names = new String[2][]; //Assign the one-dimensional array with length 3 to the position with subscript 0 in the two-dimensional array names[0] = new String[3]; //Assign the one-dimensional array with length 4 to the position with subscript 1 in the two-dimensional array names[1] = new String[4]; System.out.println("Get the number of one-dimensional arrays in the two-dimensional array:" + names.length);//2 System.out.println("Get the number of elements of the first one-dimensional array in the two-dimensional array:" + names[0].length);//3 System.out.println("Get the number of elements of the second one-dimensional array in the two-dimensional array:" + names[1].length);//4 } }