The array is a set of data with the same data type. Java supports most of the array. Each basic unit of the one -dimensional array is data of basic data type. For array, and so on, each basic unit of the N dimensional array is N-1 as an array N-1 dimension array. The following uses an array as an example to illustrate the usage of the Java array.
1. Array statement
Array declarations have the following two forms (the positions of square brackets are different):
int arr[];int[] arr2;
2. Array initialization
There are two forms of array initialization, as follows (using New or not using NEW):
int ARR [] = new int [] {1, 3, 5, 7, 9}; int [] arr2 = {2, 4, 8, 8, 10};3. Traversing an array
To traverse the array, you can use for/foreach, as follows:
public static void main(String[] args) { int arr[] = new int[]{1, 3, 5, 7,9}; int[] arr2 = {2, 4, 6, 8, 10}; for (int i = 0; i < arr.length; ++i) { System.out.print(arr[i] + "/t"); // 1 3 5 7 9 } for (int x: arr2) { System.out.print(x + "/t"); // 2 4 6 8 10 } }4. Arrays.fill() fills the array
Using the static method of the Arrays class, the import package java.util.Arrays is required, and many overloaded methods are defined.
Void Fill (int [] a, int Val) All fill in Void Fill (int [] A, int FROMINDEX, int toindex, int Val) Fill in the element Int [] arr3 = new int [5]; : arr3) { System.out.print(x + "/t"); // 0 0 0 0 0 All initialized to 0 } System.out.println(); Arrays.fill(arr3, 10); for (int x: arr3) { System.out.print(x + "/t"); // 10 10 10 10 10 All fill to 10 } System.out.println(); Arrays.fill(arr3, 1, 3, 8 ); free (int x: arr3) {system.out.print (x + "/t"); // 10 8 8 10 10 10 Fill in specified index} System.out.println ();5. Arrays.sort () Sort the array on the array
void sort (int [] a) all sorted void sort (int [] a, int FROMINDEX, int toindex) Sorting the specified indexing element int [] arr4 = {3, 7, 2, 9}; arrays.sort Arr4); for (int x: arr4) {system.out.print (x + "/t"); // 1 2 3 7 9} System.out.println (); int [] arr5 = {3, 7 , 2, 1, 9}; arrays.sort (arr5, 1, 3); for (int x: arr5) {system.out.print (x + "/t"); // 3 2 7 1 9} Systemm .out.println ();6. Arrays.copyof () Copy the array
int [] Copyof (int [] Original, int Newlenth) copy arrays, specify the length of the new array Int [] Copyofrange (int [] Original, Int from, int to), specify the index int [] of the original array of the original array of the original array. Arr6 = {1, 2, 3, 4, 5}; int []] arr7 = arrays.copyof (ARR6, 5); // 1 2 3 4 5 int [] arr8 = arrays.copyofrange (ARR6, 1, 3) ; // 2 3 for (int x: arr7) {system.out.print (x + "/t");} System.out.println (); for (int x: arr8) {system.out.print ( x + "/t");} System.out.println (); 7. Check whether the array contains a certain value
String [] stringarray = {"a", "b", "c", "d", "e", "boolean b = arrays.aslist (stringarray) .contains (" a "); System.out.println ( b); // TRUEFirst use arrays.aslist () to convert array to list <string>, so that the contains function of a dynamic linked list can determine whether the element is included in the linked list.
8. Connect two arrays
int [] Intarray = {1, 2, 3, 4, 5}; int [] Intarray2 = {6, 7, 8, 9, 10}; // Apache Commons Libraryint [] CombineDInTILS.Addall (Intarray ,, Intarray2);ArrayUtils is an array processing class library provided by Apache. The Addall method can easily connect two array into an array.
9. Array flip
int [] Intarray = {1, 2, 3, 4, 5}; Arrayutils.reverse (INTARRAY); System.out.println (Arrays.tostrary (Intarray)); // [5, 4, 3, 2, 1 ]Still use the universal Arrayutils.
10. Remove an element from an array
int [] Intarray = {1, 2, 3, 4, 5}; int [] Removed = Arrayutils.RemoveElement (INTARRAY, 3); // Create a new arraySystem.Println (Arrays.tostringRing ( Removed);