java multidimensional array

1. Overview of multidimensional arrays

Array: it is a linear data structure used to store a set of data of the same data type (the length is immutable once defined).

Requirements: store the scores of 5 students in one class.

double[] scores = new double[5];
scores[0] = 90;
// ....

Advanced requirements: store the results of 5 students in each of the three classes.

double[] scores1 = new double[5];
double[] scores2 = new double[5];
double[] scores3 = new double[5];
// ...

Multidimensional array: it can be understood as a nested array.

Common forms are: two-dimensional array, three-dimensional array

Two dimensional array: it is an array with one-dimensional array as array elements, that is, "array of arrays".

2. Multidimensional array definition

Syntax of one-dimensional array:

The data type of the array element[] Array name = new The data type of the array element[Capacity of the array/length/Number of elements];
The data type of the array element[] Array name = {Element value 1, Element value 2, ...};
The data type of the array element[] Array name = new The data type of the array element[] {Element value 1, Element value 2, ...};

// For example: store the scores of 5 students
double[] scores = new double[5];
double[] scores = {90, 80, 70, 60, 50};
double[] scores = new double[] {90, 80, 70, 60, 50};

// Assignment syntax
 Array name[subscript] = Element value;

// For example: store the grade of the first student
scores[0] = 90;

From the perspective of memory allocation principle, they are all one-dimensional arrays. They are arrays with one-dimensional arrays as array elements, that is, "array of arrays".

Syntax of two-dimensional array:

  • A one-dimensional array is stored in a two-dimensional array, so the type of one-dimensional array is used as the number of array elements of a two-dimensional array

    According to the type.

    The data type of the array element[] Array name = new The data type of the array element[Capacity of array/length/Number of elements];
    The data type of the array element[] Array name = {Element value 1, Element value 2, ...};
    The data type of the array element[] Array name = new The data type of the array element[] {Element value 1, Element value 2, ...};
    
    // For example: store the scores of 5 students in 3 classes
    double[][] scores = new double[3][5]; 
    double[][] scores = {{90, 80, 70, 60, 50}, {...}, {...}};
    double[][] scores = new double[][] {{90, 80, 70, 60, 50}, {...}, {...}};
    
    // Assignment syntax
     Array name[Outer dimension array subscript][Inner dimension array subscript] = Element value;
    
    // For example: store the grade of the first classmate for the first class
    scores[0][0] = 90;

Note: when defining a two-dimensional array, the capacity of the inner dimension array can be omitted.

For example:

double[][] scores = new double[3][];
scores[0] = new double[5];
scores[1] = new double[2];
scores[2] = new double[3];

This method is more flexible than directly specifying the inner dimension array capacity during definition. Inner dimension arrays can specify different array capacities.

3. Traversal of multidimensional array

Single loop is used to solve the traversal of one-dimensional array.

Use multiple loops to solve the traversal of multidimensional arrays.

// Store the scores of 5 students in 3 classes
double[][] scores = new double[3][5];

Scanner input = new Scanner(System.in);
for (int i = 0; i < scores.length; i++) {
    System.out.printf("Please enter page%d Grade of each class:\n", i+1);
    for (int j = 0; j < scores[i].length; j++) {
        System.out.printf("Please enter%d Class No%d Scores of students:", i+1, j+1);
        scores[i][j] = input.nextDouble();
    }
}

System.out.println();

// ergodic
for (int i = 0; i < scores.length; i++) {
    System.out.printf("The first%d The results of each class are listed as follows:\n", i+1);
    for (int j = 0; j < scores[i].length; j++) {
        System.out.println(scores[i][j]);
    }
}

4. Use of multidimensional arrays

Refer to class code

5. Algorithm overview

Algorithm (Algorithm) refers to the accurate and complete description of the problem-solving scheme. It is a series of clear instructions to solve the problem. The Algorithm represents the strategy mechanism to describe the problem-solving in a systematic way. In other words, it can obtain the required output for a certain standard input in a limited time. Different algorithms may use different time, space or efficiency to complete the same task . The advantages and disadvantages of an Algorithm can be measured by space complexity and time complexity.

A recipe is an algorithm. We have a preliminary definition of the concept of "algorithm": an algorithm is the process of solving a problem.

 6. Sorting algorithm

The so-called sorting algorithm is to reorder one or more groups of data according to the established pattern through a specific algorithmic factor. This new sequence follows certain rules and reflects certain laws. Therefore, the processed data is convenient for screening and calculation, and the calculation efficiency is greatly improved. For sorting, we first require it to have a certain stability, that is, when two identical elements appear in a sequence at the same time, after a certain sorting algorithm, the relative positions of the two before and after sorting will not change. In other words, even two identical elements are different in the sorting process, and confusion is not allowed.

Sorting algorithms also have many solutions. Common ones are: bubble sorting, selection sorting, insertion sorting, quick sorting, heap sorting, merge sorting, Hill sorting, cardinal sorting, etc.  

6.1 bubble sorting

It repeatedly visits the element column to be sorted, compares two adjacent elements in turn, and exchanges them if the order (e.g. from large to small, and the initial letter from Z to A) is wrong

For example: 9, 7, 8, 6, 2, sort in ascending order.

Bubble sorting: outer cycle n-1, inner cycle n-1-i

6.2 selection and sorting

It is similar to the idea of finding the minimum value in the challenge arena.

6.3 insert sort

It is similar to comparing, inserting new data, and maintaining the existing sequence in an existing sequence.

7. Reference data type

There are 8 [basic] data types in Java:

Value:

  • Integer: byte, short, int, long

  • Floating point numbers: float, double

Non numeric:

  • char

  • boolean

Reference data type:

  • array

  • Class (String is a class,...)

  • Interface

  • enumeration

 

 

Keywords: Java

Added by MarcAndreTalbot on Sun, 19 Dec 2021 03:15:08 +0200