Array of java basic syntax (including bubble sorting)

catalogue

array

Definition of array:

One dimensional array

Array creation:

Suggestions for use:

Array access:

Array length

Traversal of array:

Two dimensional array

Traversal of two-dimensional array:

Arrays and methods

Common problems in using arrays

Memory partitioning in Java

1 stack memory

2. Heap memory:

3. Method area:

4. Local method stack:

5. Register:

Bubble sort:

array

Definition of array:

Open up a suitable space (a series of continuous spaces) in the memory to store multiple data values of the same type at the same time;

One dimensional array

A set of data of the same type can be stored

Declaration array:

Open up a suitable space (a series of contiguous spaces) in memory

characteristic:

1. Reference data type

2. Multiple data types in the array must be unified

3. Once the length of the array is defined, it cannot be changed;

Array creation:

1. Dynamic initialization:

Format of dynamically initialized array:

Data type [] array name = new data type [array length];

double[] arrays = new double[6];

[] represents an array

new represents the action of creating an array

2. Static initialization:

Format of static initialization array:

Data class new [] array name = new data class new [] {element 1, element 2, element 3...};

Omit format:

Data class new [] array name = {element 1, element 2, element 3...};

int[] arrays = new int[]{1,2,3,4};
//Equivalent to
int[] arrays = {1,2,3,4};

matters needing attention:

1. The array length is not defined, but the array will automatically define the array length according to the number of elements;

2. The static initialization standard format can be disassembled, and the omitted format cannot be disassembled;

Suggestions for use:

Static initialization can be used when the element is determined, but dynamic initialization can only be used when the element is uncertain;

Array access:

Directly print out the array name to get the hash value of the memory address corresponding to the array;

Format of access array:

Array name [index value]

Index: subscript of array element of type int;

be careful:

Index value range (0 ~ array length - 1)

When dynamically initializing an array, its elements will automatically have a default value. The rules are as follows:

Shaping: default 0

Floating point: default 0.0

Character type: default '\ u0000'

Boolean: the default is false

Reference type: null by default

matters needing attention:

Static initialization will also have default values, but will be directly replaced by the elements in braces;

Array length

Array name length

Once the array length is defined, it cannot be changed during program operation.

Traversal of array:

The elements in the array are processed one by one;

Two dimensional array

It is essentially a one-dimensional array, and each element of it is a one-dimensional array

public class Demo11Array {
    public static void main(String[] args) {
 / / define a two-dimensional array
        int[][] arr1=new int[4][5];
​
        int[][] arr2=new int[2][];
        arr2[0]=new int[2];
        arr2[1]=new int[3];
​
        //3.
        int[][] arr3={{1,3,4},{2,3}};
​
​
​
    }
}
​

Traversal of two-dimensional array:

Nesting of for loops to be used;

Arrays and methods

Arrays can be used as arguments to methods

Code example

public class Demo9ArrayParam {
    public static void main(String[] args) {
        int[] array = {10, 23, 45, 78, 89};
//        System.out.println(array);
//        printArray(array);
​
        int[] cal = cal(13, 24);
        System.out.println("And results:"+cal[0]);
    }
}

An array can be used as the return value of a method

A method can have 0, 1 and multiple parameters, but the return value can only have 0 or 1, and cannot have multiple return values

If you want multiple results from a method to be returned,

Solution: use the array as the return value type

 Code example:
public static int[] cal(int a, int b) {
        int sum = a + b;
        int mul = a - b;
​
        int[] arr = new int[2];
        arr[0] = sum;
        arr[1] = mul;
​
        return arr;
   }

Method nameexplain
boolean equals(array1,array2)Compare whether array1 and array2 arrays are equal
sort(array)Arrange the elements of the array in ascending order
String toString(array)Converts an array array to a string
void fill(array,val)Assign all elements of the array to val
copyOf(array,length)Copy the array array into a new array with length
int binarySearch(array,val)Query the subscript of the element value val in the array (the array must be arranged in ascending order)

Code example:

package cn.zhm;
​
import java.util.Arrays;
​
/**
 * Arrays class
 */
public class Demo10Arrays {
    public static void main(String[] args) {
        //equals(): compares whether two arrays are equal
        int[] arr1 = {10, 20, 30, 40};
        int[] arr2 = {10, 20, 30, 40};
        int[] arr3 = {10, 30, 20, 40};
​
        boolean result = Arrays.equals(arr1, arr3);
        System.out.println(result);
​
        //sort(): sort the array elements in ascending order
        Arrays.sort(arr3);
​
        for (int i : arr3) {
            System.out.print(i + "\t");
        }
​
        System.out.println();
        //toString()
        System.out.println(Arrays.toString(arr3));
        //fill()
​
        Arrays.fill(arr3, 40);
        System.out.println(Arrays.toString(arr3));
        //copyOf()
        int[] newArr = Arrays.copyOf(arr2, 5);
        System.out.println(Arrays.toString(newArr));
​
        //binarySearch()
        int[] nums={45,34,67,89,56};
        Arrays.sort(nums);
        int index = Arrays.binarySearch(nums, 45);
        System.out.println(index);
​
​
    }
}

Common problems in using arrays

Array index out of bounds exception.

The index number of the array ends from 0 to length - 1

If the number does not exist when accessing the array, the array index is out of bounds

All reference type variables can be assigned a null value, representing nothing

Memory partitioning in Java

1 stack memory

All stored are local variables of the method;

Local variable: method parameter or internal variable of method ();

2. Heap memory:

All new contents are in heap memory;

The contents of heap memory have a hexadecimal address

The data in the heap memory has a default value;

3. Method area:

When storing class wants to close the information, including method information

4. Local method stack:

Operating system related

5. Register:

CPU related

 

Bubble sort:

analysis:

Compare two adjacent numbers each time and exchange them

Outer loop to control the number of comparison rounds;

The inner loop controls the times of each round of comparison;

package cn.ybk;
​
/**
 * Bubble sorting
 *
 */
public class DemoBubbleSort {
    public static void main(String[] args) {
        //Define an array
        int[] arrays = {100,98,78,32,23,87,65};
        int temp;//Define a temporary variable for exchange
        for (int i=1;i<arrays.length;i++){
            //Outer cycle comparison rounds
            for(int j=0;j<arrays.length-i;j++){
                //The inner loop controls the number of comparisons per round
                if(arrays[j]>arrays[j+1]){
                    temp=arrays[j];
                    arrays[j]=arrays[j+1];
                    arrays[j+1]=temp;
                }
            }
        }
        //Print
        for (int array : arrays) {
            System.out.print(array + "\t");
        }
    }
}

Keywords: Java Eclipse intellij-idea

Added by dervaish on Mon, 03 Jan 2022 06:35:18 +0200