Array of Java
Definition of array
1. An array is an ordered collection of data of the same type
2. Array describes several data of the same type, which are arranged and combined in a certain order
3. One of the data is called an array element, and each array element can be accessed through a subscript
Array declaration creation
1. Array variables must be declared before they can be used in programs. Syntax for declaring array variables:
dataType[ ] arrayRefvar; // be the first choice
Or dataType arrayRefvar[]// Mostly used for C and C++
2. Java language uses the new operator to create arrays, syntax;
dataType[ ] arrayRefvar =new dataType[arraySize];
3. Array elements are accessed by index, which starts from 0
4. Get array length: arrays length
Code example:
package com.method; public class ArrayDomen { public static void main(String[] args) { //Type of variable name of variable = value of variable; //Array type int[] nums;//1. Declare an array nums = new int[10];//2. Create an array; int[] nums2 = new int[10];//Another way to declare the creation of arrays; //3. Assign values to array elements nums[0] = 1; nums[1] = 2; nums[2] = 3; nums[3] = 4; nums[4] = 5; nums[5] = 6; nums[6] = 7; nums[7] = 8; nums[8] = 9; nums[9] = 10; //Calculate the sum of all elements int sum =0; for (int i=0; i< nums.length; i++){ sum = sum +i; } System.out.println("the sum:"+sum); } }
Output results:
45
Dynamic and static initialization
Code example:
package com.method; public class ArrayDomen02 { public static void main(String[] args) { //Static initialization: create + assign; int[] a = {1,2,3,4,5,7}; System.out.println(a[0]); //Dynamic initialization; Include default initialization int[] a2 = new int[10]; a2[0] = 1; a2[1] = 2; System.out.println(a2[0]); System.out.println(a2[1]); System.out.println(a2[2]); System.out.println(a2[3]); } }
Output results:
1 1 2 0 0
Four basic characteristics of arrays
1. Its length is definite. Once an array is created, its size cannot be changed.
2. Its elements must be of the same type, and mixed types are not allowed
3. The elements in the array can be any data type, including basic type and reference type
4. Array variables belong to the reference type, and arrays can also be regarded as the member variables of each element in the object array
5. The array itself is an object, and the object in Java is in the heap. Therefore, whether the array keeps the original type or other object types, the array object itself is in the heap.
Array boundary
1. The legal range of the subscript is [0, length-1], and an error will be reported if it exceeds the range;
As follows;
public static void main(String [ ] args){
int [] a = new int[2];
System.out.printIn(a[2]);
}
ArrayIndexOutOfBoundsException: array subscript exception;
2. Array summary
- An array is an ordered set of the same data type;
- Arrays are also objects. The array element is equivalent to the member variable of the object;
- The length of the array is certain and cannot be changed. If it is out of bounds, an ArrayIndexOutOfBoundsException will be reported
Use of arrays
basic:
Code example:
package com.array; public class ArrayDomento1 { public static void main(String[] args) { int[] arrays = {1,2,3,4,5,6,7,8,9}; //Print all array elements for (int i = 0; i< arrays.length; i++){ System.out.print(arrays[i]+"\t"); } System.out.println("======================"); //Calculate the sum of all elements int sum = 0; for(int i = 0; i< arrays.length; i++){ sum = sum +arrays[i]; } System.out.println("sum="+sum); System.out.println("======================"); //Find the largest element int max = arrays[0]; for (int i = 1; i < arrays.length; i++){ if (arrays[i]>max){ max = arrays[i]; } } System.out.println("max="+max); System.out.println("======================"); } }
Output results:
1 2 3 4 5 6 7 8 9 ====================== sum=45 ====================== max=9 ======================
Advanced:
- For Each cycle;
Code example:
package com.array; public class ArrayDoment02 { public static void main(String[] args) { int [] arrays = {1,2,3,4,5}; //Enhanced array loop, but cannot get subscript; for (int array : arrays) {//Array is equivalent to the value of each array, and arrays is an array System.out.println(array); } } }
Output results;
1 2 3 4 5
- Array as method input parameter
Code example:
package com.array; public class ArrayDoment02 { public static void main(String[] args) { int [] arrays = {1,2,3,4,5}; printArray(arrays); } //Printout array public static void printArray(int[] arrays){ for (int i = 0; i < arrays.length; i++){ System.out.print(arrays[i]+" "); } } }
Output results:
1 2 3 4 5
- Array as return value
Code example:
package com.array; public class ArrayDoment02 { public static void main(String[] args) { int [] arrays = {1,2,3,4,5};//Step 1: pass the value of the argument to the formal parameter // printArray(arrays); int [] x = reverse(arrays);//Step 4: traverse the output of the returned result for (int array:x) { System.out.println(array); } } public static int[] reverse(int[] arrays){//Step 2: call the method after passing in the argument int[] result = new int[arrays.length]; for (int i = 0,j = result.length-1; i < arrays.length; i++,j--){ result[j] = arrays[i]; //Assign the first element of i to the last element of j } return result;//Step 3: after executing the method body, return to X } }
Output results:
5 4 3 2 1
Multidimensional array
Two dimensional array:
Code example: traversing a two-dimensional array
package com.array; public class ArrayDoment05 { public static void main(String[] args) { int[][] array = {{1,2},{2,3},{3,4},{4,5},{5,6},{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]+"\t"); } } }
Output results:
1 2 2 3 3 4 4 5 5 6 6 7