1. Creating a one-dimensional array
The simplest and quickest way to do this is to allocate memory to the array at the same time as the declaration. Such as:
int Month[]=new int[12]
You can also declare and redistribute memory first. Such as:
int month[]; or int[] month; Month=new int[12];
There are two methods that can be used when initializing a one-dimensional array. Such as:
int arr[]=new int[]{1,2,3,4,5}; Or: int arr[]={1,2,3,4,5};
2. Creating a two-dimensional array
There are two ways to create a two-dimensional array in the same way as a one-dimensional array. Such as:
First declare the redistribution int arr[][]; Arr=new int[2][4]; Direct Assignment int arr[][]=new int[2][4];
Note: Allocating memory can be assigned as a per-dimension assignment, and is commonly used in irregular arrays. Such as:
int a[][]; A=new int[2][]; A[0]=new int[4]; A[1]=new Int[5];
For the operation of a two-dimensional array, learn to use a foreach statement to traverse a two-dimensional array.
int arr[][]={{2,3},{3,7},{6,5}}; for (int x[]:arr) {for (int e:x) { system.out.print (e+ ");} }
3. Array manipulation
3.1 Filling elements
After importing Java.util.Arrays, use the Fill () method. Such as:
Arrays.fill (arr,8);
Arrays.fill (arr,1,2,8); Padding between first and second (not included) elements 8
3.2 Sort
After importing Java.util.Arrays, use the Arrays.sort () method. Such as:
int arr[]={2,5,1,7,6}; Arrays.sort (arr);
3.3 Copying an array
Two ways to use the Arrays class:
CopyOf (): Copies the array to the specified length array. Such as:
int arr[]={21,45,46}; int newarr[]=arrays.copyof (arr,5); The newarr array is: 21,45,46,0,0
Copyofrange (): Copies the specified length array elements to the new array. Such as:
int arr[]={1,4,5,7,8,9}; int Newarr[]=arrays.copyofrange (arr,0,3);
3.4 Array Query
The BinarySearch () method of the Arrays class provides multiple overloads that can be searched using a binary lookup method. Such as:
BinarySearch (Object[],key), BinarySearch (object[],int fromindex,int toindex,int key).
3.5 Array Sorting algorithm
Master the Java program writing of the basic sorting algorithm such as bubble sort, direct selection sorting and so on. Let's take a bubble sort example and write the algorithm yourself in Java:
public class bubblesort{public void Main (string[] args) { int arr[]={2,4,8,5,6,10,1}; Create an instance of Bubblesort, call the method sort order Bubblesort sorter=new bubblesort (); Sorter.sort (arr); public void sort (Int. array[]) {for (int. i=0;i<array.length (); i++) {for (int j=array.length () -2;j>i;j--) { if (Array[j+1]<array[j]) { int temp=array[j+1]; ARRAY[J+1]=ARRAY[J]; array[j]=temp; } } } }
Learning Note--java Array