1. Basic definition of array
Definition format of array:
Dynamic initialization of array:
Declare and initialize an array:
- Data type array name [] = new data type [length];
- Data type [] array name = new data type [length];
Static initialization of array: set the contents of the array when it is defined;
- Simplified format: data type array name [] = {data 1, data 2, data 3};
- Complete format: data type array name [] = new data type [] {data 1, data 2, data 3,...};
After creating an array, you can use it as follows:
Array access can access each element through the footmark. The footmark is defined from 0, so you can use the footmark range: "0 ~ array length - 1". If the footmark range is exceeded, the array will be out of bounds.
- Array is used for convenient variable management, so the for loop is often used to complete the array operation.
- For the length of an array, you can also use "array name. Length".
Dynamic initialization of array:
public class ArrayDemo{ public static void main(String args[]){ int data [] = new int [3]; data[0]=12; data[1]=65; data[2]=89; for (int x=0;x<data.length;x++){ System.out.println(data[x]); } } }
Static initialization of array:
public class ArrayDemo{ public static void main(String args[]){ int data [] = new int []{12,65,89}; for (int x=0;x<data.length;x++){ System.out.println(data[x]); } } }
2. Array reference passing analysis
Through the basic definition of the array, it can be found that the keyword new is still needed for memory development in the process of using the array. Similarly, there must be memory matching.
If an array is a reference data type, reference transfer will occur. The way of reference transfer: a heap memory can be pointed to by multiple stack memories.
Case study:
public class ArrayDemo{ public static void main(String args[]){ int data [] = new int []{10,20,30}; int temp [] =data;//Reference passing temp[0]=99; for (int x=0;x<data.length;x++){ System.out.println(data[x]); } } }
Because the array is of reference type, it must be used only after the heap memory space is opened. If the array that opens the heap memory space is used now, the error of "NullPointerException" will appear. Therefore, the array operation can be carried out in the form of subscript only when an instantiated object is provided.
3. foreach output
In order to avoid the array out of bounds exception if the subscript is not handled properly, it can be obtained directly by using the syntax structure of foreach.
Syntax: for (data type variable: array | set) {}
The biggest feature is that the content of each element in the array can be automatically taken out and saved in the variable, so that the array content can be obtained directly through the variable without subscript acquisition.
public class ArrayDemo{ public static void main(String args[]){ int data [] = new int []{1,2,3,4,5}; for (int temp : data){ //Automatic cycle System.out.println(temp); } } }
4. Two dimensional array
For two-dimensional arrays, you can use the following definition syntax:
Dynamic initialization of array:
- Data type array name [] = new data type [number of rows] [number of columns];
Static initialization of array:
- Data type array name [] [] = new data type [] []
{data, data,...}, {data, data,...};
public class ArrayDemo{ public static void main(String args[]){ int data [][] = new int [][]{{1,2,3,4,5},{1,2,3},{5,6,7,8}}; for (int x=0;x<data.length;x++){ for(int y=0;y<data[x].length;y++){ System.out.println("data["+x+"]["+y+"] ="+data[x][y]); } System.out.println(); } } }
5. Arrays and methods
Reference transfer case:
public class ArrayDemo{ public static void main(String args[]){ int data [] = new int [] {1,2,3,4,5}; printArray(data); //Pass array } public static void printArray(int temp[]){ for (int x =0;x<temp.length;x++){ System.out.println(temp[x]); } } }
Since you can receive a number through a method, you can also return an array object through a method. At this time, you only need to control the return value type of the method.
public class ArrayDemo{ public static void main(String args[]){ int data [] = initArray();//The contents of the array can be obtained through the method printArray(data); //Pass array } public static int [] initArray(){ int arr [] = new int [] {1,2,3,4,5}; return arr;//Returns an array } public static void printArray(int temp[]){ for (int x =0;x<temp.length;x++){ System.out.println(temp[x]); } } }
Example: modify the contents of an array through a method
public class ArrayDemo{ public static void main(String args[]){ int data [] = new int [] {1,2,3,4,5}; changeArray(data);//Modify array contents printArray(data); //Pass array } public static void changeArray(int arr[]){ for (int x =0;x<arr.length;x++){ arr[x] *= 2; } public static void printArray(int temp[]){ for (int x =0;x<temp.length;x++){ System.out.println(temp[x]); } } }
Case: arbitrarily define an int array. It is required to calculate the sum, average, maximum and minimum of the array elements. Try not to have too much client code in the main class, and use only the main method in the main class.
class ArrayUtil{ private int sum; private double avg; private int max; private int min; public ArrayUtil(int data[]) { for (int x= 0;x<data.length;x++) { if (data[x]>max) { this.max = data[x]; } if (data[x]<min) { this.min=data[x]; } this.sum += data[x]; } this.avg = this.sum / data.length; } public int getSum() { return this.sum; } public double getAvg() { return this.avg; } public int getMax() { return this.max; } public int getMin() { return this.min; } } public class Hello{ public static void main(String args[]) { int data [] = new int [] {1,2,3,4,5}; ArrayUtil arr = new ArrayUtil(data); System.out.println("Array and:"+arr.getSum()); System.out.println("Array average:"+arr.getAvg()); System.out.println("Array maximum:"+arr.getMax()); System.out.println("Array minimum:"+arr.getMin()); } }
6. Array sorting case analysis
Sort by ascending sort.
Case code:
class ArrayUtil{ public static void sort(int data[]) { for (int x =0;x<data.length;x++) { for (int y = 0;y<data.length - x-1;y++) { if (data[y] >data[y+1]) { int temp = data[y]; data[y] = data[y+1]; data[y+1] = temp; } } } } public static void printArray(int temp[]) { for (int x = 0;x<temp.length;x++) { System.out.print(temp[x]+","); } System.out.println(); } } public class Hello{ public static void main(String args[]) { int data [] = new int [] {8,9,0,2,3,5,11,78,14,6,1}; ArrayUtil.sort(data);//sort ArrayUtil.printArray(data); } }
7. A case study of array transpose
Case study 1: reverse 1, 2, 3, 4, 5, 6, 7, 8 and 9
class ArrayUtil{ public static void printArray(int temp[]) { for (int x = 0;x<temp.length;x++) { System.out.print(temp[x]+","); } System.out.println(); } } public class Hello{ public static void main(String args[]) { int data [] = new int [] {1,2,3,4,5,6,7,8,9}; int temp[] = new int [data.length]; int foot = temp.length - 1; for (int x= 0; x<data.length;x++) { temp[foot --] = data[x]; } data = temp; ArrayUtil.printArray(data); } }
Take 1, 2 and 3 as examples:
Case study 2: transpose in an array
class ArrayUtil{ public static void printArray(int temp[]) { for (int x = 0;x<temp.length;x++) { System.out.print(temp[x]+","); } System.out.println(); } } public class Hello{ public static void main(String args[]) { int data [] = new int [] {1,2,3,4,5,6,7,8,9}; int center = data.length / 2; int head = 0; int tail = data.length - 1; for (int x =0 ;x<center;x++) { int temp = data [head]; data [head] = data [tail]; data [tail] = temp; head++; tail--; } ArrayUtil.printArray(data); } }
If the two implementations are to be compared, it can be found that the first processing method has more cycles and generates garbage, while the second implementation reduces the number of cycles, increases the time complexity of if judgment, but can reduce the generation of useless objects to improve performance.
Change the conversion function to a class definition:
class ArrayUtil{ public static void reverse(int data[]) { int center = data.length / 2; int head = 0; int tail = data.length - 1; for (int x =0 ;x<center;x++) { int temp = data [head]; data [head] = data [tail]; data [tail] = temp; head++; tail--; } } public static void printArray(int temp[]) { for (int x = 0;x<temp.length;x++) { System.out.print(temp[x]+","); } System.out.println(); } } public class Hello{ public static void main(String args[]) { int data [] = new int [] {1,2,3,4,5,6,7,8,9}; ArrayUtil.reverse(data); ArrayUtil.printArray(data); } }
8. Method variable parameters
Case: define a method that can add any number of integer data.
Traditional practices:
class ArrayUtil{ public static int sum(int [] data) { int sum = 0; for (int temp : data) { sum += temp; } return sum; } } public class Hello{ public static void main(String args[]) { System.out.println(ArrayUtil.sum(new int [] {1,2,3,4})); } }
Because this code can only pass arrays, not multiple numbers, the following method variable parameters are used.
class ArrayUtil{ public static int sum(int ... data) {//Variant array int sum = 0; for (int temp : data) { sum += temp; } return sum; } } public class Hello{ public static void main(String args[]) { System.out.println(ArrayUtil.sum(1,2,3,4)); System.out.println(ArrayUtil.sum(new int [] {1,2,3,4})); } }
The biggest function of variable parameters is that when some program classes are designed or called by developers in the future, the transfer operation of array can be avoided by using this form, but the essence of variable parameters needs to be clear: they still belong to array.
9. Object array
Previously, we have been exposed to arrays defined by basic data types, but in the Java program itself, various data types can become arrays, and such arrays are called object arrays.
The object array is defined as follows:
- Dynamic initialization: class object array name [] = new class [length], and the content of each element is null;
- Static initialization: class object array name [] = new class [] {instantiated object, instantiated object...}
Instance: dynamically initialize an array
class Person{ private String name; private int age; public Person(String name,int age) { this.name = name; this.age = age; } public String getInfo() { return "full name:"+this.name+",Age:"+this.age; } // setter and getter omitted } public class Hello{ public static void main(String args[]) { Person per [] = new Person[3];//object array per[0] = new Person("Zhang San",20); per[1] = new Person("Li Si",20); per[2] = new Person ("Wang Wu",20); for (int x= 0;x<per.length ;x++) { System.out.println(per[x].getInfo()); } } }
Instance: static initialization of object array
class Person{ private String name; private int age; public Person(String name,int age) { this.name = name; this.age = age; } public String getInfo() { return "full name:"+this.name+",Age:"+this.age; } // setter and getter omitted } public class Hello{ public static void main(String args[]) { Person per [] = new Person[]{//object array new Person("Zhang San",20), new Person("Li Si",20), new Person ("Wang Wu",20)}; for (int x= 0;x<per.length ;x++) { System.out.println(per[x].getInfo()); } } }