JAVA Foundation (day 9) array tool class

catalogue

1 array

1.1 concept

1.2 creating arrays

1.3 analysis of array creation process

1.4 length of array

Exercise 1.5: storing data into an array hello

1.6 traversal of array

1.7 exercise-1: output the number of days per month

1.8 exercise-2: traverse the array and store 1 to 10

1.9 exercise-3: creating a random array

2 array tool class Arrays

2.1 arrays.tostring (array)

2.2 arrays.sort (array)

2.3 exercise: exercise arrays.sort (array)

2.4 arrays.copyof (array, new length)

2.5 exercise: exercise arrays. Copyof (array, new length)

3 extended two-dimensional array

3.1 concept

3.2 creating a two-dimensional array

3.3 traversing a two-dimensional array

3.4 printing data in two-dimensional array

1 array



1.1 concept

Array, marked with [], is used to store multiple sets of data of the same type
To get the element value in the array, you can get it through the footmark (subscript)
The array subscript starts from 0, and the maximum value of the subscript is the length of the array minus 1

1.2 creating arrays

Array creation methods are generally divided into dynamic initialization and static initialization

  1. dynamic initialization
    int[] a = new int[5];
  2. initiate static
    int[] b = new int[]{1,2,3,4,5};
    int[] c = {1,2,3,4,5};

1.3 analysis of array creation process


The program creates an array int[] a = new int[5]; What happened at the time?

Open up a continuous space in memory to store data. The length is 5
Complete the initialization process for the array, give each element a default value, and the default value of int type is 0
When the array is initialized, a unique address value is assigned
Give the unique address value to the reference type variable a to save


Array name is a variable of reference type. It stores the address of the array, not the data in the array

1.4 length of array

The length of the array is represented by the length attribute. Once the array is created, the length cannot be changed
The length of the array is allowed to be 0



Exercise 1.5: storing data into an array hello

Create package: cn.tedu.array
Create class: TestCreateArray.java

package cn.tedu.array;

import java.util.Arrays;

/**This class is used to practice the creation of arrays*/
public class TestCreateArray {
    public static void main(String[] args) {
        //1. Create an array
        //1.1 static Creation -- you already know the value of each element when you create it
        char[] c1 = {'h','e','l','l','o'};
        char[] c2 = new char[] {'h','e','l','l','o'};
        
        //1.2 dynamic creation -- after knowing the length of the array, we will assign a specific value later
        char[] c3 = new char[5];//Open up a continuous memory space in memory to store 5 characters
        //1.2.2 dynamic assignment to c3 array
        /**We manipulate each element in the array through the index of the array. Note that the index of the array starts from 0*/
        c3[0] = 'h';//Assign a value to the first element in the array
        c3[1] = 'e';//Assign a value to the second element in the array
        c3[2] = 'l';//Assign a value to the third element in the array
        c3[3] = 'l';//Assign a value to the fourth element in the array
        c3[4] = 'o';//Assign a value to the fifth element in the array
        
        //2. Print and view the array just created
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
        
        //3. Create an array of String type to store data "a","b","c"
        String[] s1 = {"a","b","c"};
        String[] s2 = new String[] {"a","b","c"};
        String[] s3 = new String[3];
        s3[0] = "a";//Assign "a" to the first element of the s3 array
        s3[1] = "b";//Assign "b" to the second element of the s3 array
        s3[2] = "c";//Assign "c" to the third element of the s3 array
        
        /**char The bottom layer of the array of type is processed, and the specific elements in the array can be printed directly
         * For Arrays other than char type, if you want to view the specific elements in the array, you need to use the tool class Arrays of the array
         * Specific method: arrays.tostring (array name);
         * Note that you need to import packages when using Arrays*/
        //4. Print the created array
        System.out.println(s1);//What is printed out is the address value of the array
        System.out.println(Arrays.toString(s2));
        
        //5. View the length of the array -- the number of elements stored in the array
        /**Once the array is created, the length cannot be changed. If you want to add / delete elements in the array
         * You can only create a new length array first, and then copy the original data*/
        System.out.println(c1.length);
        System.out.println(s1.length);
        
    }
    
}


 

1.6 traversal of array


Traversal: access each position of the array from beginning to end to obtain the elements of each position. The form is as follows:
We operate the array through the index of the array, so the for loop variable also operates on the index of the array

Start: start subscript 0 end: end subscript length-1 how to change:++
For (starting from the position with subscript 0; subscript value < = length of array - 1; subscript + +) {undefined
Circulatory body;
}`
 

1.7 exercise-1: output the number of days per month


Create class: TestArrayExec.java

package cn.tedu.array;
/**This class is used to practice traversal of arrays
 * Demand: output the number of days of each month in a year
 * */
public class TestArrayExec {
    public static void main(String[] args) {
        m ();//This method is used to output the days of each month. The shortcut key for quick repair is Ctrl+1
    }
    //Printing requirements: for example, there are 31 days in January
    public static void m () {
        //1. Create an array to store the days of 12 months
        int[] a = {31,28,31,30,31,30,30,31,30,31,30,31};
        
        //2. Print the days of each month to the console
        //The traversal of the array -- through a loop -- operates on the subscript of the array
        //Loop start: a[0] - the first element in the array
        //End of loop: a[a.length-1] - the last element in the array
        //How to change:++
        //for(int i = 0 ; i <=a.length-1 ; i++) {
        for(int i = 0 ; i <a.length ; i++) {
            //a[i] - obtain the value of the element at the corresponding position in array a according to the subscript I
            System.out.println((i+1)+"Yue you:"+a[i]+"day");
        }
    }
}

1.8 exercise-2: traverse the array and store 1 to 10

//Requirements: traverse the array and store it in 1-10
    public static void m2() {
        //1. Create array -- dynamic creation
        int[] a = new int[10];
        
        //2. Traverse the array and assign values to each position in turn
        //Loop start: a[0] - the first element in the array
        //End of loop: a[a.length-1] - the last element in the array
        //How to change:++
        for(int i = 0 ; i <= a.length-1 ; i++) {
            //a[i] - obtain the value of the element at the corresponding position in array a according to the subscript I
            a[i] = i+1;
        }//End of for loop
        //3. Print a array after storing 10 data
        System.out.println(a);//[I@15db9742
        //In addition to char type Arrays, other types of Arrays need to use the tool class Arrays to view specific contents
        //Arrays.tostring (the name of the array to be printed) to view the specific contents of the array
        System.out.println(Arrays.toString(a));//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

1.9 exercise-3: creating a random array

demand:Create array,The contents of the array are random numbers within 100

public static void m3() {
    //1. Create array -- Dynamic
    int[] a = new int[10];
    //2. Traverse the array and assign values to each element in the array
    for(int i = 0 ; i <a.length ; i++) {
        //100 is user-defined data, indicating that the range of generated random integers is [0100)
        a[i] = new Random().nextInt(100);
        //a[i] = new Random().nextInt(100)+1; / / both before and after the value range + 1 -- > [1101)
    }
    //3. Use the tool class of the array to view the elements in the array
    System.out.println(Arrays.toString(a));

     

2 array tool class Arrays



2.1 arrays.tostring (array)

Put the data in the array,Concatenate a comma into a string[Value 1,Value 2]

   

2.2 arrays.sort (array)

Sort arrays,For arrays of basic types, the optimized quick sort algorithm is used,efficient
 Array of reference types,The optimized merge sorting algorithm is used

2.3 exercise: exercise arrays.sort (array)


Create class: testarraysort.java

package cn.tedu.array;

import java.util.Arrays;

/**This class is used to test the tool class Arrays of Arrays*/
public class TestArraysSort {
    public static void main(String[] args) {
        //1. Create an unordered array
        int[] a = {21,96,75,23,25};
        
        //The sorting algorithm used at the bottom is the optimized quick sorting algorithm
        Arrays.sort(a);//Sort unordered arrays directly
        System.out.println(Arrays.toString(a));
    }

2.4 arrays.copyof (array, new length)

Assigns an array to a new array of the specified length
The length of the new array is longer than the original array, which is equivalent to copying and increasing the position
The length of the new array is smaller than that of the original array, which is equivalent to intercepting part of the data

2.5 exercise: exercise arrays. Copyof (array, new length)


Create class: TestArraysCopyOf.java

package cn.tedu.array;

import java.util.Arrays;

/**This class is used to practice shrinking and expanding arrays*/
public class TestArraysCopyOf {
    public static void main(String[] args) {
        //1. Create an array
        int[] from = {1,2,3,4,5};//Once the array is created, its length cannot be changed
        
        //2.1 common copy of array
        /**copyOf()Used to copy the array. Two parameters:
         * Parameter 1: which array do you want to copy
         * Parameter 2: length of new array*/
        int[] to = Arrays.copyOf(from, 5);
        System.out.println(Arrays.toString(to));
        
        //2.2 expansion of array
        /**Capacity expansion: expand the capacity of the array. The length of the new array > the length of the original array
         * Capacity expansion idea: first create a new array with corresponding length, and the default value is 0 in each position
         * Then copy the elements from the original array to the new array. The elements that are not overwritten still have the default value of 0*/
        int[] to2 = Arrays.copyOf(from, 10);
        System.out.println(Arrays.toString(to2));
        
        //2.3 shrinking of array
        /**Shrink: reduce the capacity of the array. The length of the new array is less than the length of the original array
         * Shrinkage idea: first create a new array with corresponding length, and the default value is 0 at each position
         * Then copy the specified number of elements from the original array to the new array, similar to interception*/
        int[] to3 = Arrays.copyOf(from, 3);
        System.out.println(Arrays.toString(to3));
        
        //2.4 specify to intercept the elements in the original array from beginning to end
        /**copyOfRange()It is used to intercept the array. There are three parameters:
         * Parameter 1: which array to intercept [original array]
         * Parameter 2: which subscript of the original array to start from
         * Parameter 3: to which subscript of the original array ends
         * Note: the intercepted element contains the element at the start subscript, not the element at the end subscript*/
        int[] to4 = Arrays.copyOfRange(from, 2, 4);
        System.out.println(Arrays.toString(to4));
    }
}

3. Expansion Two dimensional array



3.1 concept

The array that holds the array, that is, the data stored in the array is still in the form of array

3.2 creating a two-dimensional array


int[][] a = {undefined{3,5},{7,9},{1,2}};
– create an external array with a length of 3
– create an internal array for the position of each external array, and the length of each internal array is 2
– data initialization for each internal array
– the two-dimensional array generates unique address values
– give the address value to the reference type variable a to save

3.3 traversing a two-dimensional array


For (int i = 0; I < a.length; I + +) {/ / traverses the external array
For (int j = 0; J < a [i]. Length; j + +) {/ / traverse the internal array
System.out.println(a[i][j]); / / print the values of each element in the two-dimensional array in turn
}
}

3.4 printing data in two-dimensional array
 

package cn.tedu.array;

import java.util.Arrays;

/**This class is used to print data in a two-dimensional array*/
public class TestArrays2 {
    public static void main(String[] args) {
        //1. Create a two-dimensional array
        //    3      5    
        //a[0][0] a[0][1]
        //    7       9
        //[1][0] a[1][1] 
        //    1       2      3
        //a[2][0] a[2][1] a[2][2]
        int[][] a = {{3,5},{7,9},{1,2,3}};
        
        //2. Traverse two-dimensional array
        for (int i = 0; i < a.length; i++) {
//            System.out.println(Arrays.toString(a[i]));
            for (int j = 0; j < a[i].length; j++) {
                //a[i][j] - locate specific elements according to the subscript of the external array and the subscript of the internal array
                System.out.print(a[i][j]);
            }
            System.out.println();
        }
    }
}

Tags: Java Back-end

Posted on Sun, 07 Nov 2021 14:54:13 -0500 by dbrimlow