On Array Base Operations

1. Overview of arrays
During the execution of a program, a large amount of data is often stored.
Java and many advanced languages provide a data structure called an array that can be used to store an ordered set of fixed number of elements with the same element type. Array mainly solves the storage problem of multivariable and multidata, and facilitates the unified maintenance of operation data in the later stage of the program. What is the nature of arrays? An array is a series of equally sized, contiguous storage spaces. Why is space the same size? In order to maintain our data uniformly, we must ensure that the types of data are the same. Why are the addresses of variable spaces continuous? Address contiguous tangent sizes are equal to facilitate the calculation of specific physical memory addresses for subsequent elements.

An array is a piece of storage where the addresses are continuous and the size of the space is consistent, but each space contains the addresses of other data. Arrays exist in heap memory, but any data stored in the heap is called an object. Arrays provide subscripts to access elements in the array. An array variable stores the address of the first element of the array in heap memory. The array accesses elements through subscripts by calculating the address of the data to be accessed = the address of the first element + the subscript + the size of the data type. Once an array is defined, its length cannot be changed. How many addresses are in the array? Just see how many element spaces the array has (the length of the array). The size or content must be explicitly specified when creating an array.
2. Use of one-dimensional arrays
Creating an array specifies only the length but not the content

int []array=new int[4];

** Create the specified contents of the array (the length is determined at the same time)

int []array=new int[]{12,313,23};
int[]array={12,313,23};

Different types of arrays have different default values

Each element in the array can be referenced only after space has been defined and allocated by the operator new;
References to array elements; Array name [array element subscript]
Array element subscripts can be integer constants or integer expressions. Such as a[3],b[i],c[6*1];
Array element subscripts start at 0; The range of subscript values for combinatorial method with length n is 0 - > n-1; Such as int a[]=new int[3]; The referenced array elements are a[0], a[1], a[2]
Each array has an attribute, length, that indicates its length. For example, a.length indicates the length (number of elements) of the array a.
Once an array is initialized, its length is immutable
3. One-dimensional Array Operations
(1) Array traversal
Rolling table method:

public class $Testllll {
    public static void main(String[] args) {
        int []array={12,31,23,25,64,13};
        int max=array[0],maxIndex=0;
        for(int i=1;i<array.length;i++){
            if(array[i]>max) {
                max = array[i];
                maxIndex=i;
            }
        }
        System.out.println(max+"\n"+maxIndex);
    }
}

Find if an element exists in the array:

import java.util.Scanner;
public class $Testllll {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int []array={12,31,32,52,45,31};
        System.out.println("Please enter the number you are looking for");
        int num=scanner.nextInt();
        int numindex=0;
        for(int i=0;i<array.length;i++){
            if(num==array[i]){
                numindex=1;
                break;
            }
        }
        if(numindex==1) {
            System.out.println(numindex);
        }else
            System.out.println("Non-existent");
    }
}

Array Add Element

import java.util.Arrays;
import java.util.Scanner;
public class $Testllll {
    public static void main(String[] args) {
       String []array={"tony","mike","lily","jack","allen"};
       Scanner scanner=new Scanner(System.in);
       String str=scanner.nextLine();
       String[]newarray=new String[array.length+1];
       for(int i=0;i<newarray.length;i++) {
           if (i == newarray.length - 1) {
               newarray[newarray.length-1] = str;
           } else {
               newarray[i] = array[i];
           }
       }
       array=newarray;
       System.out.println(Arrays.toString(array));
    }
}

Method copyOf in Java

String[] newarr= Arrays.copyOf(array, array.length+1);
newarr[newarr.length-1]=addname;

Array Delete Element

import java.util.Arrays;
import java.util.Scanner;
public class $Testllll {
    public static void main(String[] args) {
       String []array={"tony","mike","lily","jack","allen"};
       Scanner scanner=new Scanner(System.in);
       String str=scanner.nextLine();
       String[]newarray=new String[array.length-1];
       for(int i=0;i<array.length;i++) {
           if (array[i].equals(str)) {
               for(int j=0;j<newarray.length;j++) {
                   if (j<i) {
                       newarray[j] = array[j];
                   } else {
                       newarray[j] = array[j + 1];
                   }
               }
               array=newarray;
               System.out.println(Arrays.toString(array));
           }
       }
       System.out.println("No results found");
    }
}

arraycopy method

String []array={"tony","mike","lily","jack","allen"};
       String[]newarray=new String[array.length-1];
       System.arraycopy(array,3,newarray,0,2);
       System.out.println(Arrays.toString(newarray));

Keywords: Java Algorithm data structure leetcode

Added by hailam on Thu, 03 Feb 2022 19:55:45 +0200