[crazy God says JAVA notes] P51~P59 Arrays, multidimensional Arrays, Arrays class (to_string, sort, fill)

Video online class: station B, crazy God says JAVA: P51~P59 array

array

1. Definition of array in Java

The following two array definitions are OK, and the first one is more recommended.

int[] nums; //Only arrays are declared and no memory space is allocated
int nums2[];

2. Use new in java language to create a new array

//The first method
int nums[] = new int[10];
//The second method
int[] nums;
nums = new int[10];

3. Array elements are accessed through subscripts. The length of the array can be obtained directly through the length attribute

 int[] nums; //Declaration array
 nums = new int[10];//Allocate memory space

 System.out.println(nums.length);//Length of output array

When watching the online class, I casually typed a use case for reference:

public class TestArrays {
    public static void main(String[] args) {
        int[] nums;
        nums = new int[10];
        for (int i = 0; i < 10; i++) {
            nums[i]=i*35+27;
        }
        System.out.println("The current array length is:"+nums.length);
        System.out.println("The current array contents are:");
        for (int j = 0; j < 10; j++) {
            System.out.println(" "+nums[j]);
        }
    }
}

4.JAVA memory analysis

JAVA memory can be divided into heap, stack and method area.
Among them, the heap can store new objects and arrays, which can be shared by all threads and will not be referenced by other objects
The stack can store the basic variable type (which will contain the specific value of this type) and the variable of the reference object (which will store the specific address of the reference in the heap);
The method area, which can be shared by all threads, contains all class and static variables

java objects are in the heap, so the array object itself is in the heap whether it saves the original type or other object types.

5. Array subscript out of bounds

The legal range of array subscript is [0, array.length-1]. If it exceeds this range, an array subscript out of bounds exception will be generated.
This generates an ArrayIndexOutofBoundException error

6. Use of arrays

6.1 for each cycle

A way to quickly traverse an array: [IDE shortcut key: array name. for]
This is a subscript free loop, so it is only suitable for printing and not for complex operations.

for (int num : nums) {
      System.out.println(nums);
        }

6.2 array as function parameter and calling method

public class TestArrays {
    public static void main(String[] args) {
        int[] nums;
        nums = new int[10];
        for (int i = 0; i < 10; i++) {
            nums[i] = i * 35 + 27;
        }
        System.out.println("Calculate the sum of all elements:");
        printArraySum(nums);  //Pass parameters when calling a function

    }
    public static void printArraySum(int[] num){ //As an argument to a function
        int sum=0;
        for(int i=0;i<num.length;i++)
        {
            sum+= num[i];
        }
        System.out.println(" "+sum);
    }
}

7. Multidimensional array

Multidimensional arrays can be treated as arrays of arrays.

int[][] array = {{1,2},{2,3},{3,4},{4,5}};  //Statically defined two-dimensional array

7.1 Length attribute of multidimensional array

Multidimensional arrays also have the length attribute——
For example, an array:

int[][] nums; //Declare a two-dimensional array
nums = new int[2][3];//Define a 2D array

That is, the two-dimensional array stores two arrays, each of which has a space of 3.
That is:

System.out.println("First dimension array length:"+nums.length);
System.out.println("Second dimension array length:"+nums[0].length);

Output 2 first, then output 3.

7.2 parameter transfer of multidimensional array

public class TestArrays {
    public static void main(String[] args) {
        int[][] nums2;
        nums2 = new int[2][3];
        printtwoNums(nums2);//Multidimensional array as parameter
    }
    public static void printtwoNums(int[][] nums3){ //Multidimensional array as parameter
        System.out.println("First dimension array length:"+nums3.length);
        System.out.println("Second dimension array length:"+nums3[1].length);
    }
}

8. Arrays class - a class that handles arrays in JAVA

Remember to import the package before using:

import java.util.Arrays;

8.1 Arrays printing array elements tostring

import java.util.Arrays;//Import package

public class Test2Arrays {
    public static void main(String[] args) {
        //Define the array and assign initial values
        int[] nums;
        nums = new int[10];
        for (int i = 0; i < 10; i++) {
            nums[i]=i*35+27;
        }

        //The Arrays method prints array elements_ Parentheses before and after
        System.out.println(Arrays.toString(nums));
    }
}

Output results:

8.2 array sorting of arrays - sort

Here, in order to facilitate the observation and sorting, a process of generating random numbers is added. First, let the program generate random numbers, fill the array, and then sort it.
You need to import a math package first, and then use the random number method in it.

import java.util.Arrays;//Import package
import java.math.*; //Import a Math package to generate random numbers

public class Test2Arrays {
    public static void main(String[] args) {
        //Define the array and assign initial values
        int[] nums;
        nums = new int[10];
        for (int i = 0; i < 10; i++) {
            nums[i]=(int)(Math.random()*100);//Randomly generate random numbers within 100 and convert them to integers
        }
        for (int i = 0; i < 10; i++) {
            System.out.print(" "+nums[i]);
        }

        //The Arrays method
        Arrays.sort(nums);
        System.out.println("The sorted array is:");
        //Print array
        System.out.println(Arrays.toString(nums));
    }
}

Output results:

8.3 fill method of arrays class

Fill all / part of the elements in the array with a fixed value

import java.util.Arrays;//Import package
import java.math.*; //Import a Math package to generate random numbers

public class Test2Arrays {
    public static void main(String[] args) {
        //Define the array and assign initial values
        int[] nums;
        nums = new int[10];
        for (int i = 0; i < 10; i++) {
            nums[i]=(int)(Math.random()*100);//Randomly generate random numbers within 100 and convert them to integers
        }
        for (int i = 0; i < 10; i++) {
            System.out.print(" "+nums[i]);
        }

        //The Arrays method
        Arrays.sort(nums);
        System.out.println("The sorted array is:");
        //Print array
        System.out.println(Arrays.toString(nums));

        System.out.println("Fill all elements in the array with 0:");
        Arrays.fill(nums, 0);//Fills all elements in the array with zeros
        for(int i=0;i<nums.length;i++)
            System.out.print(" "+nums[i]);
    }
}

Output results:

Tags: Java array

Posted on Wed, 13 Oct 2021 21:41:57 -0400 by tommyinnn