[Java Basics] Array Application and Source Code Analysis

Series Article Directory

[Java Basics] StringBuffer and StringBuilder class application and source code analysis
[Java Basics] array application and source code analysis
[Java Basics] String, analyze memory address, source code



foreword

Arrays are one of the important data structures for every programming language. Of course, different languages ​​have different implementations and processing of arrays.
Arrays provided in the Java language are used to store elements of the same type with a fixed size.
You can declare an array variable such as numbers[100] instead of directly declaring 100 independent variables number0, number1, ..., number99.
This tutorial will introduce you to the declaration, creation and initialization of Java arrays, and give the corresponding codes.

1. Declare array variables

Array variables must first be declared before the array can be used in the program. Following is the syntax for declaring array variables:

dataType[] arrayRefVar;   // 首选的方法


dataType arrayRefVar[];  // 效果相同,但不是首选方法

Note: It is recommended to use the declaration style of dataType[] arrayRefVar to declare array variables. The dataType arrayRefVar[] style comes from the C/C++ language, and it is adopted in Java to allow C/C++ programmers to quickly understand the Java language.
Here are code examples for both syntaxes:

double[] myList;         // 首选的方法
double myList[];         //  效果相同,但不是首选方法

2. Create an array

The Java language uses the new operator to create arrays, the syntax is as follows:

arrayRefVar = new dataType[arraySize];

The above syntax statement does two things:
● 1. An array is created using dataType[arraySize].
● 2. Assign the reference of the newly created array to the variable arrayRefVar.

The declaration of an array variable, and the creation of an array can be done with a single statement, as follows:

dataType[] arrayRefVar = new dataType[arraySize];

In addition, you can also create arrays in the following ways.

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

The elements of the array are accessed by index. Array indices start at 0, so index values ​​range from 0 to arrayRefVar.length-1.
The following statement first declares an array variable myList, then creates an array containing 10 double type elements, and assigns its reference to the myList variable.

public class TestArray {
    
     
    public static void main(String[] args) {
    
    
        // 数组大小
        int size = 10;
        // 定义数组
        double[] myList = new double[size];
        myList[0] = 1.2;
        myList[1] = 2.3;
        myList[2] = 3.4;
        myList[3] = 4.5;
        myList[4] = 5.6;
        myList[5] = 6.7;
        myList[6] = 7.8;
        myList[7] = 8.9;
        myList[8] = 99.10;
        myList[9] = 10000;
        System.out.println("数组: " + Arrays.toString(myList));
    }
}

The output of the above example is:

Array: [1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 99.1, 10000.0]

3. For-Each loop, print the data in the array

This instance is used to display all elements in the array myList:

public class TestArray {
    
    
    public static void main(String[] args) {
    
    
        double[] myList = {
    
    1.9, 2.9, 3.4, 3.5};

        // 打印所有数组元素
        for (double element: myList) {
    
    
            System.out.println(element);
        }
    }
}

4. Arrays tool class

The java.util.Arrays class facilitates manipulation of arrays, and all methods provided by it are static.
Has the following functions:

  • Assignment to the array: through the fill method.
  • Sort the array: by sort method, in ascending order.
  • Comparing arrays: use the equals method to compare whether the element values ​​in the array are equal.
  • Find array elements: through the binarySearch method, binary search operations can be performed on the sorted array.
serial number Methods and instructions
1 public static int binarySearch(Object[] a, Object key)
uses the binary search algorithm to search for an object (Byte, Int, double, etc.) of a given value in a given array. The array must be sorted before calling. Returns the index of the search key if the lookup value is contained in the array; otherwise returns (-(insertion point) - 1).
2 public static boolean equals(long[] a, long[] a2)
Returns true if the two specified long arrays are equal to each other. Two arrays are considered equal if they contain the same number of elements and all corresponding pairs of elements in both arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. The same approach works for all other primitive data types (Byte, short, Int, etc.).
3 public static void fill(int[] a, int val)
assigns the specified int value to each element in the specified range of the specified int type array. The same approach works for all other primitive data types (Byte, short, Int, etc.).
4 public static void sort(Object[] a)
sorts the specified object array in ascending order according to the natural order of its elements. The same approach works for all other primitive data types (Byte, short, Int, etc.).

This instance is used to sort the array myList:

public class TestArray {
    
    
    public static void main(String[] args) {
    
    
        double[] myList = {
    
    5.2, 1.9, 1.7, 4.4, 3.5};

        // 打印所有数组元素 - 排序前
        for (double element: myList) {
    
    
            System.out.println(element);
        }
        System.out.println("-----------------");
        Arrays.sort(myList);
        // 打印所有数组元素 - 排序后
        for (double element: myList) {
    
    
            System.out.println(element);
        }

    }
}

5. Array operation

Array addition, deletion, query, and modification operations

public static void main(String[] args) {
    
    
        // 数组大小
        int size = 10;
        // 定义数组
        Integer[] myList = new Integer[size];
        //增加数据
        for (int i = 0; i < size; i++) {
    
    
            myList[i] = new Random().nextInt(100);
        }
        System.out.println("数组: " + Arrays.toString(myList));
        System.out.println("数组数据长度: " + myList.length );
        System.out.println("--------------------------------");

        //删除数据
        myList[8] = null;
        System.out.println("数组: " + Arrays.toString(myList));
        System.out.println("数组数据长度: " + myList.length );
        System.out.println("--------------------------------");

        //修改数据
        myList[8] = 100;
        System.out.println("数组: " + Arrays.toString(myList));
        System.out.println("数组数据长度: " + myList.length );
        System.out.println("--------------------------------");

    }

Guess you like

Origin blog.csdn.net/s445320/article/details/131576957