Java array

Arrays provided in the Java language are used to store fixed-size elements of the same type.

1. Declare array variables

Array variables must be declared before arrays can be used in a program.

dataType[] arrayRefVar; // The preferred method dataType arrayRefVar[]; // Has the same effect, but is not the preferred methodCopy the code

It is recommended to declare array variables in the declarative style of dataType[] arrayRefVar. The dataType arrayRefVar[] style is derived from the C/C++ language and was adopted in Java to enable C/C++ programmers to quickly understand the Java language. Example:

double[] myList; // The preferred method double myList[]; // Has the same effect, but is not the preferred methodCopy the code

2. Create an array

The Java language uses the new operator to create arrays

arrayRefVar = new dataType[arraySize];
Copy the code

The above syntax statement does two things:

An array is created using dataType[arraySize].

Assigns a reference to the newly created array to the variable arrayRefVar.

Declaration of array variables and creation of arrays can be done with a single statement like this:

dataType[] arrayRefVar = new dataType[arraySize];
Copy the code

Alternatively, you can create arrays as follows.

dataType[] arrayRefVar = {value0, value1, ... , valuek};Copy the code

The elements of an array are accessed by index. The array index starts at 0, so the index value goes from 0 to ArrayRefvar.length-1. The following statement first declares an array variable myList, then creates an array of 10 elements of type double, and assigns its references to the myList variable.

Public class TestArray {public static void main(String[] args) {// Array size int size = 10; Double [] myList = new double[size]; MyList [0] = 5.6; MyList [1] = 4.5; MyList [2] = 3.3; MyList [3] = 13.2; MyList [4] = 4.0; MyList [5] = 34.33; MyList [6] = 34.0; MyList [7] = 45.45; MyList [8] = 99.993; myList[9] = 11123; Double total = 0;for (int i = 0; i < size; i++) {
         total += myList[i];
      }
      System.out.println("The sum is:"+ total); }}Copy the code
The total is 11367.373Copy the code

The following image depicts the array myList. There are 10 double elements in myList, with indices from 0 to 9.

3. Work with arrays

The element type of an array and the size of the array are determined, so when dealing with array elements, we usually use a basic loop or a for-each loop. This example completely shows how to create, initialize, and manipulate arrays:

Public class TestArray {public static void main(String[] args) {double[] myList = {1.9, 2.9, 3.4, 3.5}; // Prints all array elementsfor (int i = 0; i < myList.length; i++) {
         System.out.println(myList[i] + ""); } double total = 0;for (int i = 0; i < myList.length; i++) {
         total += myList[i];
      }
      System.out.println("Total is "+ total); Double Max = myList[0]; double Max = myList[0];for (int i = 1; i < myList.length; i++) {
         if (myList[i] > max) max = myList[i];
      }
      System.out.println("Max is "+ max); }}Copy the code
1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5
Copy the code

4. The for-each loop

JDK 1.5 introduced a new type of loop, called a for-each loop or enhanced loop, that can iterate over groups of numbers without using subscripts.

for(type element: array) {
    System.out.println(element);
}
Copy the code

Example:

Public class TestArray {public static void main(String[] args) {double[] myList = {1.9, 2.9, 3.4, 3.5}; // Prints all array elementsfor(double element: myList) { System.out.println(element); }}}Copy the code

The results are as follows:

1.9
2.9
3.4
3.5
Copy the code

5. Array as argument to function

Arrays can be passed to methods as arguments. Examples are as follows:

public static void printArray(int[] array) {
  for (int i = 0; i < array.length; i++) {
    System.out.print(array[i] + ""); }}Copy the code

The following example calls printArray to print 3,1,2,6,4, and 2:

printArray(new int[]{3, 1, 2, 6, 4, 2});
Copy the code

6. Array as return value of function

public static int[] reverse(int[] list) {
  int[] result = new int[list.length];
 
  for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
    result[j] = list[i];
  }
  return result;
}
Copy the code

7. Multidimensional arrays

A multidimensional array can be regarded as an array of arrays. For example, a two-dimensional array is a special one-dimensional array where each element is a one-dimensional array. For example, String STR [][] = new String[3][4];

1. Dynamic initialization of multidimensional arrays (taking two-dimensional arrays as an example)

Allocate space directly for each dimension

type[] []typeName = new type[typeLength1][typeLength2];
Copy the code

Type can be a basic data type or a composite data type. Arraylength1 and ArrayLENGTH2 must be positive integers. Arraylength1 is the number of rows and ArrayLENGTH2 is the number of columns. 2. Starting from the highest dimension, allocate space for each dimension, for example:

String s[][] = new String[2][];
s[0] = new String[2];
s[1] = new String[3];
s[0][0] = new String("Good");
s[0][1] = new String("Luck");
s[1][0] = new String("to");
s[1][1] = new String("you");
s[1][2] = new String("!");
Copy the code

S [0]=new String[2] and S [1]=new String[3] allocate reference space for the highest dimension, i.e., limit the maximum length of data that can be stored for the highest dimension, and then allocate space separately for each of its array elements s0=new String(” Good “) and other operations.

A reference to a multidimensional array (using a two-dimensional array as an example)

For each element in a two-dimensional array, reference arrayName[index1][index2]

8. Arrays class

The Java.util. Arrays class makes it easy to manipulate Arrays, and all the methods it provides are static. It has the following functions:

To assign an array: use the fill method.

Sort arrays: By sort method, in ascending order.

Compare arrays: Compares the values of the elements in an array using equals.

Find array elements: binarySearch can be used to find sorted arrays.

Feel free to leave a comment below if you don’t understand