1. Three ways to define arrays
Array: a collection that stores a set of data of the same data type
public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5,6,7}; // Array defined and initialization [] cannot put any numbers // matters needing attention: // 1. When defining an array, specific numbers cannot be written, int[] array2 = new int[3]; // Elements with the a size of the 3 are not initialized. Default is 0 // new is the keyword: instantiate an object. It means that an array in Java is also an object int[] array3 = new int[]{1,2,3,4,5}; } }
2. Array length, access element
public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5,6,7}; System.out.println(array.length); // Current array length System.out.println(array[3]); // Accessing elements with subscript 3 // System.out.println(array[-1]); // ArrayIndexOutOfBoundsException: -1 array out of bounds exception array[3] = 10; // change System.out.println(array[3]); } }
3. Three forms of print array
① Traversal array:
public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; for (int i = 0; i < array.length; i++) { System.out.print(array[i]+" "); } System.out.println(); } }
② Enhanced for loop / for each loop:
public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; for (int x : array) { // Receive each element of the array System.out.print(x+" "); } System.out.println(); }
Difference between for loop and each loop:
The for loop can get the subscript, and the for each can only get the element (which is much used in the collection)
③ With the help of Java's operation array tool class Arrays:
import java.util.Arrays; public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; // import java.util.Arrays; String ret = Arrays.toString(array); // Outputs the return value String of the current array as a String System.out.println(ret); // [1, 2, 3, 4, 5, 6] System.out.println(Arrays.toString(array)); } }
4. Null pointer exception
public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5,6,7}; int[] array2 = null; // Null reference this reference does not point to any object // System.out.println(array2.length); // Null pointer exception // System.out.println(array2[0]); // Null pointer exception } }
5. How do I print arrays?
public class TestDemo { public static void printf(int[] array) { // How do I print arrays? for (int i = 0; i < array.length; i++) { System.out.print(array[i]+" "); } System.out.println(); } public static void main(String[] args) { int[] array = {1,2,3,4,5,6,7}; printf(array); } }
6,func1 / func2
import java.util.Arrays; public class TestDemo { public static void func1(int[] array) { array = new int[]{3,4,5,6,7}; } public static void func2(int[] array) { array[0] = 900; } public static void main(String[] args) { int[] array = {1,2,3,4,5,6,7}; System.out.println(Arrays.toString(array)); func1(array); func2(array); System.out.println(Arrays.toString(array)); } }
7. Quote
public class TestDemo { public static void main(String[] args) { int[] array1 = {1,2,3,4,5}; int[] array2 = array1; int[] array = new int[]{1,2,3,4,5}; //array = new int[10]; //array = new int[2]{1,2}; //array = new int[5]; } }
- Represents that the reference array2 points to the object pointed to by the reference array1
Reference to reference: this sentence is wrong. A reference can only point to an object
A reference cannot point to multiple objects at the same time - The reference is not necessarily on the stack:
Whether a variable is on the stack or not is determined by the nature of the variable,
If it's a local variable, it's on the stack
Example member variables are not necessarily on the stack
8. Example
1. Digital exchange
public class TestDemo { public static void swap(int[] array) { int tmp = array[0]; array[0] = array[1]; array[1] = tmp; } public static void main(String[] args) { int[] array = {10, 20}; System.out.println("Before exchange:"+array[0]+" "+array[1]); swap(array); System.out.println("After exchange:"+array[0]+" "+array[1]); } }
2. Double the original array
import java.util.Arrays; public class TestDemo { public static void func(int[] array) { // Expand in element group for (int i = 0; i < array.length; i++) { array[i] = 2 * array[i]; } } public static int[] transform(int[] array) { // Changing the new array does not affect the original array int[] ret = new int[array.length]; for (int i = 0; i < array.length; i++) { ret[i] = 2 * array[i]; } return ret; } public static void main(String[] args) { // Takes an array as the return value of a method int[] array = {1,2,3,4,5}; // func(array); // System.out.println(Arrays.toString(array)); int[] ret = transform(array); System.out.println(ret); } }
3. Analog implementation of array to string toString
import java.util.Arrays; public class TestDemo { public static String myToString(int[] array) { // [1,2,3,4,5] if(array == null) return "null"; String str = "["; for (int i = 0; i < array.length; i++) { str = str + array[i]; if(i != array.length - 1) { str = str + ","; } } str = str + "]"; return str; } public static void main(String[] args) { int[] array = {1,2,3,4,5}; // System.out.println(Arrays.toString(array)); System.out.println(Arrays.toString(array)); } }
4. Find the maximum value of the array
public class TestDemo { public static int maxNum(int[] array) { if(array == null) return -1; if(array.length == 0) return -1; int max = array[0]; for (int i = 1; i < array.length; i++) { if(max < array[i]) { max = array[i]; } } return max; } public static void main(String[] args) { int[] array = {12, 22, 3 ,7 ,13}; System.out.println(maxNum(array)); } }
5. Find the specified element (binary search)
import java.util.Arrays; public class TestDemo { public static int findNum(int[] array, int key) { for (int i = 0; i < array.length; i++) { if(array[i] == key) { return i; } } return -1; // The subscript has no negative number } public static int binarySearch(int[] array, int key) { int left = 0; int right = array.length - 1; while(left <= right) { int mid = (left + right) / 2; if(array[mid] > key) { right = mid -1; }else if(array[mid] < key) { left = mid + 1; }else { return mid; } } return -1; } public static void main(String[] args) { int[] array = {3, 7, 13, 12, 22}; // System.out.println(findNum(array, 7)); // Binary search - ordered System.out.println(binarySearch(array, 17)); } }
6. Given an integer array, judge whether the array is ordered (ascending)
import java.util.Arrays; public class TestDemo { public static boolean isSortedUp(int[] array) { for (int i = 0; i < array.length - 1; i++) { if(array[i] > array[i+1]) { return false; } } return true; } public static void main(String[] args) { int[] array = {1,2,3,10,5,6}; boolean flag = isSortedUp(array); System.out.println(flag); // System.out.println(isSortedUp(array)); } }
7. Given an array, let the array be sorted in ascending (descending) order
import java.util.Arrays; public class TestDemo { public static void bubbleSort(int[] array) { for (int i = 0; i < array.length - 1; i++) { boolean flag = true; for (int j = 0; j < array.length - 1 - i; j++) { if(array[j] > array[j+1]) { int tmp = array[j]; array[j] = array[j+1]; array[j+1] = tmp; flag = false; } } if(flag == true) { break; } } } public static void main2(String[] args) { // 1. Bubble sorting int[] array = {9, 5, 2, 7}; bubbleSort(array); // 2. Arrays.sort Arrays.sort(array); for (int x : array) { System.out.print(x+" "); } } }
8. Given an array, arrange the elements in reverse order
Two subscripts pointing to the first element and the last element respectively. Exchange elements in two positions
Then let the previous subscript increase and the latter subscript decrease, and the cycle can continue
import java.util.Arrays; public class TestDemo { public static void reverse(int[] array) { int left = 0; int right = array.length -1; while (left < right) { int tmp = array[left]; array[left] = array[right]; array[right] = tmp; left++; right--; } } public static void main(String[] args) { int[] array = {1, 2, 3, 4}; reverse(array); System.out.println(Arrays.toString(array)); } }
9. Given an integer array, put all even numbers in the first half and all odd numbers in the second half of the array
Set two subscripts to point to the first element and the last element respectively
Find the first odd number from left to right with the previous subscript, find the first even number from right to left with the latter subscript, and then exchange the elements of the two positions
Cycle in turn
import java.util.Arrays; public class TestDemo { public static void transform(int[] array) { int left = 0; int right = array.length -1; while(left < right) { // At the end of the loop, left points to an odd number while(left < right && array[left] % 2 == 0) { left++; } // At the end of the loop, right points to an even number while(left < right && array[right] % 2 != 0) { right--; } int tmp = array[left]; array[left] = array[right]; array[right] = tmp; } for(int i = 0; i < array.length; i++) { System.out.print(array[i]+" "); } } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6}; transform(arr); System.out.println(Arrays.toString(arr)); } }
10. Arrays.fill
import java.util.Arrays; public class TestDemo { public static void main(String[] args) { int[] array = new int[10]; Arrays.fill(array, 99); // Fill 10 99 Arrays.fill(array, 2, 6, 1999); // From 2 subscript to 5 subscript [2,6) System.out.println(Arrays.toString(array)); } }
9. Array copy
1. for loop
import java.util.Arrays; public class TestDemo { public static int[] copyArray(int[] array) { int[] ret = new int[array.length]; for (int i = 0; i < array.length; i++) { ret[i] = array[i]; } return ret; } public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; int[] copy = copyArray(array); System.out.println(Arrays.toString(copy)); } }
2. Arrays.copyOf
import java.util.Arrays; public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; int[] copy1 = Arrays.copyOf(array, array.length); System.out.println(Arrays.toString(copy1)); // [1, 2, 3, 4, 5, 6] int[] copy2 = Arrays.copyOf(array, array.length*2); System.out.println(Arrays.toString(copy2)); // [1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0] /** * public static int[] copyOf(int[] original, int newLength) { * int[] copy = new int[newLength]; * System.arraycopy(original, 0, copy, 0, * Math.min(original.length, newLength)); * return copy; * } */ } }
3. Arrays.copyOfRange copy local
import java.util.Arrays; public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; int[] copy = Arrays.copyOfRange(array, 1, 3); // Left closed right open System.out.println(Arrays.toString(copy)); // [2, 3] } }
4. arraycopy
import java.util.Arrays; public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; int[] copy = new int[array.length]; System.arraycopy(array, 0, copy5,0, array.length); System.out.println(Arrays.toString(copy)); /** * C++/C Realized * Fast speed * public static native void arraycopy(Object src, int srcPos, * Object dest, int destPos, * int length); */ } }
5. array.clone
import java.util.Arrays; public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; int[] copy = array.clone(); // Generate a copy System.out.println(Arrays.toString(copy)); } }
Light copy deep copy
Deep copy: after the copy is completed, the access modification will not affect the original copy
Deep copy or shallow copy is man-made
To achieve deep copy, you need to copy the object
It depends on what you copy and how you copy it
10. Two dimensional array
1. Definition and access
import java.util.Arrays; public class TestDemo { public static void main(String[] args) { int[][] array = {{1, 2, 3}, {5, 6, 7}}; int[][] array2 = new int[][]{{1, 2, 3}, {5, 6, 7}}; int[][] array3 = new int[2][3]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { System.out.print(array[i][j]+" "); } System.out.println(); } System.out.println("======================"); System.out.println(array.length); // 2 System.out.println(array[0].length); // 3 for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]+" "); } System.out.println(); } System.out.println("======================"); for (int[] ret : array) { for (int x : ret) { System.out.print(x+" "); } System.out.println(); } System.out.println("======================"); System.out.println(Arrays.deepToString(array)); // [[1, 2, 3], [5, 6, 7]] } }
2. Irregular two-dimensional array
import java.util.Arrays; public class TestDemo { public static void main(String[] args) { int[][] array = {{1, 2}, {5, 6, 7}}; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]+" "); } System.out.println(); } /** * 1 2 * 5 6 7 */ System.out.println("======================"); // Irregular two-dimensional array int[][] array2 = new int[2][]; // Line 2 null array2[0] = new int[3]; array2[1] = new int[2]; for (int i = 0; i < array2.length; i++) { for (int j = 0; j < array2[i].length; j++) { System.out.print(array2[i][j]+" "); } System.out.println(); } /** * 0 0 0 * 0 0 */ } }