1, Array
1. Declaration array
// Two ways to declare arrays int array[]; int[] array2;// This is recommended // Open up space array = new int[3]; // Open up 3 spaces (array with length of 3) // Note: after opening up a space, the default value of this type is saved double[] array3 = new double[5];// Declare a double array of length 5 // Two ways to declare and assign values int[] array4 = {1, 2, 3}; int[] array5 = new int[]{1, 2, 3, 4}; array2 = new int[5]; // Assign a value to the first element array2[0] = 5;
2. Precautions
-
The type and length of the array cannot be modified
-
Only numeric values of the specified type can be stored in the array
-
The subscript of an array cannot exceed the maximum length of the array
3. Get array length
int[] arr = {1, 2, 3}; int length = arr.length; // Array length, result: 3
4. Array tool class Arrays
Arrays is a tool class used to operate arrays in java.util. Through this tool class, we can easily complete some operations on arrays. The following describes some methods that may be used
a. equals
Judge whether all elements are equal when two arrays are not null
int[] arr = {1, 2, 3}; int[] arr2 = {1, 2, 3}; boolean equals = Arrays.equals(arr, arr2); // Judge whether arr and arr2 are equal. Result: true
b. sort
Sort arrays
int[] arr = {1, 3, 2}; Arrays.sort(arr); // Sort arr, result: [1, 2, 3]
c. toString
Concatenate arrays into strings
int[] arr = {1, 2, 3}; System.out.println(Arrays.toString(arr)); // Concatenate the arr array into a string, and the result: [1, 2, 3]
**Note: * * if you directly output an array, the toString method of the array will be called by default. The default toString method of the array is as follows, so you may get something similar to[ I@1be6f5c3 Results
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }
d. copyOf
Copy the data of the specified length from the original array, and make up the default value of the data type for the excess
int[] arr = new int[]{1, 2, 3, 4}; int[] arr1 = Arrays.copyOf(arr, 3); // Copy 3 elements from arr and assign them to arr1 System.out.println(Arrays.toString(arr1)); // Result: [1, 2, 3]
e. fill
Assigns all elements in the array to the specified value
int[] arr = new int[3]; Arrays.fill(arr, 5); // Assign a value of 5 to all elements in the arr System.out.println(Arrays.toString(arr)); // Result: [5, 5, 5]
5. Array copy
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; int[] arr2 = new int[8]; /* Data copied from one array to another array at a specified position and length arr Is the initial array, 0 Represents that the replication starts from zero out in the following table, arr2 Is the target array, 3 Represents the location copied to the target array 4 Represents the length of data to be copied (cannot exceed the initial array size) */ System.arraycopy(arr, 0, arr2, 3, 4); System.out.println(Arrays.toString(arr2)); // Result: [0, 0, 0, 1, 2, 3, 4, 0]
2, Assemble
There are many kinds of collections in Java. Next, we will only explain the two commonly used collections (List and Map)
1. List
List is an ordered collection. We can create a list collection through the following statement
List list = new ArrayList();
However, when creating a List, we often need to specify the generic type of the List (that is, specify that the data saved in the List can only be the specified type). If the String generic type is specified below, the List can only save the data of String type
List<String> list = new ArrayList<>();
Let's introduce the commonly used List method
a. add
With add, we can add data to the List
// Adding data directly is appended to the end of the List set list.add("data"); // Result: [data] // Add data to the specified location by subscript list.add(0, "insert data"); // Result: [insert data, data]
b. size
Gets the number of elements in the List collection
list.size(); // Results: 2
c. contains
Determine whether the List collection contains an element
list.contains("data"); // Result: true
d. get
Get the data in the List collection by subscript
list.get(1); // Results: Data
e. clear
Empty the current List collection
list.clear(); // Result: []
f. addAll
Adds all the elements of another List collection to the current List collection
List<String> list2 = new ArrayList<>(); list2.add("Data 1"); list2.add("Data 2"); list2.add("Data 3"); list.addAll(list2); // Result: [data 1, data 2, data 3]
g. remove
Remove elements from the List collection
// Remove by data list.remove("Data 1"); // Result: [data 2, data 3] // Remove by subscript list.remove(1); // Result: [data2]
h. removeAll
Removes all the same elements of another List collection from the current List collection
list.removeAll(list2); // Result: []
2. Map
The data in the Map is saved in the form of key value pairs. We can create a Map in the following ways
Map map = new HashMap();
However, it is often necessary to specify generics. As follows, we specify that the key and value of Map must be of String type
Map<String, String> map = new HashMap<>();
Let's introduce the commonly used Map methods
a. put
By put ting, we can add key value pairs to the Map
map.put("Data 1", "Data 1 content"); // Result: {data 1 = data 1 content}
**Note: * * if you add the same key to the Map, the latter will overwrite the former
map.put("Data 1", "Data 1 modification"); // Result: {data 1 = data 1 modification}
b. size
Used to obtain the number of key value pairs in the Map
map.size(); // Results: 1
c. get
Gets the value corresponding to the specified key in the Map
map.get("Data 1"); // Result: data 1 modification
d. clear
Empty Map
map.clear(); // Result: {}
e. putAll
Add all key value pairs in another Map to the current Map
Map<String, String> map2 = new HashMap<>(); map2.put("Data 1", "Data 1 content"); map2.put("Data 2", "Data 2 content"); map.putAll(map2); // Result: {data 1 = data 1 content, data 2 = data 2 content}
f. remove
Deletes the specified element
map.remove("Data 1"); // result:
1, Conditional judgment
1. if
The programming language is almost a general conditional statement, but in Java, if there is only one execution statement, the brackets can be omitted (but sometimes this writing method is often not recommended for the readability of the code)
int a = 3; // if statement judgment if (a > 4) { System.out.println("a Up to 4"); } // if...else... Statement judgment if (a < 5) { System.out.println("a Less than 5"); } else { System.out.println("a Greater than or equal to 5"); } // if...else if...else statement judgment int age = 23; if (age >= 0 && age <= 8) { System.out.println("children"); } else if (age > 8 && age <= 12) { System.out.println("juvenile"); } else if (age > 12 && age <= 35) { System.out.println("youth"); } else if (age > 35 && age <= 60) { System.out.println("middle age"); } else { System.out.println("old age"); } // Omit brackets int age = 23; if (age >= 0 && age <= 8) System.out.println("children"); else if (age > 8 && age <= 12) System.out.println("juvenile"); else if (age > 12 && age <= 35) System.out.println("youth"); else if (age > 35 && age <= 60) System.out.println("middle age"); else System.out.println("old age");
2. switch
In Java, switch can match byte, short, int, char, String and enumeration types, but if the switch is of char type and the case value is int, the matching is ASCII
int a = 2; switch (a) { case 1: // Run from this line when a == 1 System.out.println(1); break;// The code block where the end is located, that is, switch case 2: System.out.println(2); break; default: // The code that runs when none of the other items match, default can be omitted System.out.println("The value you entered is illegal!"); break; } // Multiline matching switch (a) { //Input 1 or 2 is output to class a case 1: case 2: System.out.println("The class you entered is" + a + ",To go a class"); break; //Input 3 or 4 is output to shift b case 3: case 4: System.out.println("The class you entered is" + a + ",To go b class"); break; //Enter other values to c shift default: System.out.println("You belong to another class, go c class"); break; }