Basic Usage of Java Arrays

1 Basic theory of arrays

An array is the most common data structure, which is a sequence of basic types of data or objects of the same type encapsulated together with an identifier . In essence, an array is a simple linear sequence, so access is fast.

An array is a collection of data of the same data type. For example, a collection of fruits: apples, pears, peaches, grapes, etc.; a collection of programming languages: Java, Python, C/C++, C#, PHP, etc. In programming, these collections are called arrays. Each element in the array has the same data type.

2 Array Creation

2.1 Creation of one-dimensional array

2.1.1 Array declaration:
数组元素类型 数组名字[ ];
数组元素类型[ ] 数组名字;

The symbol "[ ]" indicates that the variable is an array type variable. A single "[ ]" indicates that the array to be created is a one-dimensional array.

example:

 int arr[];
 int[] arr;  //声明int型数组,数组中的每个元素都是int型数值
2.1.2 Array initialization

Initialization: It is to allocate memory space for the array in memory and store the data in it.

Static initialization:

Format:

数据类型[] 数组名=new 数据类型[]{
    
    元素1,元素2,元素3......};

example:

int[] arr=new int[]{
    
    1,2,3};
double[] arr2=new double[]{
    
    1,2,3};

Because the above expression is more complicated, there is also a simplified expression in Java.

Simplified format:

数据类型[] 数组名={
    
    元素1,元素2,元素3......};

example:

 int[] arr={
    
    1,2,3};
 double[] arr2={
    
    1,2,3};
Dynamic initialization:

Format:

数据类型[] 数组名=new 数据类型[数组的长度];

example:

int[] arr=new int[10];
double[] arr2=new double[10]; //定义数组长度为10的数组
2.1.3 Array access

forstatement:

public class test {
    
    
    public static void main(String[] args) {
    
    
        int[] arr={
    
    1,1,2,3,5,8,13,21,34};
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i]+" ");
        }
    }
}

foreachstatement:

public class test {
    
    
    public static void main(String[] args) {
    
    
        int[] arr={
    
    1,1,2,3,5,8,13,21,34};
        for (int i:arr) {
    
    
            System.out.print(i+" ");
        }
    }
}
1 1 2 3 5 8 13 21 34 

2.2 Creation of two-dimensional array

A two-dimensional array can be regarded as a special one-dimensional array, so the creation of a two-dimensional array is very similar to a one-dimensional array.

int arr[][] = {
    
    {
    
    1,2},{
    
    3,4}};//静态初始化
int arr2 = new int[2][4]    //动态初始化

forstatement:

public class test {
    
    
    public static void main(String[] args) {
    
    
        int arr[][] = {
    
    {
    
    1,2},{
    
    3,4}};

        for (int i = 0; i < arr.length; i++) {
    
    
            for (int j = 0; j < arr[i].length; j++) {
    
    
                System.out.print(arr[i][j] + " ");
            }
        }
    }
}

foreachstatement:

public class test {
    
    
    public static void main(String[] args) {
    
    
        int arr[][] = {
    
    {
    
    1,2},{
    
    3,4}};

        for (int[] x : arr) {
    
    
            for (int y : x) {
    
    
                System.out.print(anInt + " ");
            }
        }
    }
}
1 2 3 4 

2 Memory Analysis of Arrays

An array is a reference data type, and its elements can be any data type, including basic data types and reference data types. After the array length is determined, it cannot be modified. The data in the array is arranged in an orderly manner. When creating an array, a continuous space will be opened up in the memory. The array name is the first address of the array, which is the address pointing to the first element in the array.

public class test {
    
    
    public static void main(String[] args) {
    
    
        int[] arr={
    
    1,1,2,3,5,8,13,21,34};
        double[] arr1={
    
    3.14,0.681,37.78,0.0255};
        System.out.println(arr); //访问数组的地址值
        System.out.println(arr1);
    }
}
[I@119d7047
[D@776ec8df

This string of address values ​​is used as the address of the int type array stored in memory. Several of them [represent several-dimensional arrays, and one here [represents a one-dimensional array, Iwhich means a inttype, and the following number represents a hexadecimal number, and @the symbol in the middle acts as a separator.

JVM architecture:
insert image description here
Stack: The memory used when the method is running, such as mainthe method running, enters the method stack for execution.
Heap: store objects or arrays, newto create, all stored in heap memory
method area: store executable classfiles.
Native method stack: manages the invocation of native methods, and is used by the JVM when using operating system functions.
Program Counter: The place where the address of the unit where the next instruction is located is stored.
For example:
insert image description here

3 Array operations (use of Arrays)

3.1 Fill replacement array elements

After the elements in the array are defined, the elements in the array can be replaced through Arraysthe static methods of the class . fill()This method can complete the replacement of any type of array elements through various overloaded forms.
Format:

fill(int[] a,int fromIndex,int toIndex,int value)

1.a:要进行填充的数组。
2.fromIndex:要使用指定值填充的第一个元素的索引(包括)。
3.toIndex:要使用指定值填充的最后一个元素的索引(不包括)。
4.value:要分配给数组指定范围中的每个元素的值。

example:

import static java.util.Arrays.fill;
public class test {
    
    
    public static void main(String[] args) {
    
    
        int[] arr={
    
    1,1,2,3,5,8,13,21,34};
        fill(arr,2,5,1000);  //索引为2~5(不含)的数替换为1000
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i]+" ");
        }
    }
}
1 1 1000 1000 1000 8 13 21 34 

3.2 Array sorting

sort()Arrays can be sorted through static methods of the Arrays class .
Format:

Arrays.sort(object)

object是指进行排序的数组名称

example:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static java.util.Collections.sort;

public class a {
    
    
    public static void main(String[] args) {
    
    
        List<Integer> list=new ArrayList<Integer>();  //可以改变大小的ArrayList数组
        Random b=new Random();
        System.out.print("排序前的数列:"+"[");
        for (int i = 0; i < 10; i++) {
    
    
            int number=b.nextInt(100);
            list.add(number);
            sort(list);
            System.out.print(number+",");
        }
       System.out.println("]");
       System.out.println("排序后的数列:"+list);
    }
}
排序前的数列:[8,26,42,70,37,34,19,67,97,57] 
排序后的数列:[8, 19, 26, 34, 37, 42, 57, 67, 70, 97]

3.3 Return the elements of the array as strings

toString()Return the elements of the array as strings.

 import java.util.Arrays;
public class 实验 {
    
    
    public static void main(String[] args) {
    
    
        int[] arr={
    
    1,1,2,3,5,8,13,21,34};
        System.out.println(Arrays.toString(arr));
    }
}
[1, 1, 2, 3, 5, 8, 13, 21, 34]

3.4 Query array

ArraysClass binarySearch()method, you can use the binary search method to search the specified array to obtain the specified object. This method returns the index value of the element to be searched for.
Format:

binarySearch(Object[] a, int fromIndex, int toIndex, Object key)

1.a:要搜索的数组。
2.fromIndex:要搜索的第一个元素的索引(包含fromIndex)。
3.toIndex:要搜索的最后一个元素的索引(不包含toIndex)。
4.key:要搜索的值。

Returns the index of the searched value if keycontained in the array; otherwise -1 or "-" (the insertion point). The insertion point is the point at which the search key will be inserted into the array, i.e. the index of the first element greater than this key.
example:

import java.util.Arrays;
public class 实验 {
    
    
    public static void main(String[] args) {
    
    
        int[] arr={
    
    1,1,2,3,5,8,13,21,34};
        int index=Arrays.binarySearch(arr,0,5,5);
        int index1=Arrays.binarySearch(arr,0,5,10);
        System.out.println(index);
        System.out.println(index1);
    }
}
4
-6

The value of the variable in the above code indexis the index position of the element "5" in the array arr whose index is between 0 and 5. indexThe value is "5" at index position 4. The value of the variable indexis the index position of the element "10" in the array arr whose index is not within 0-5. The element "10" should be before "13", so the insertion point should be the index value 6 of the element "13", so indexthe value of is -6.

Guess you like

Origin blog.csdn.net/weixin_57038791/article/details/129350432