java-Chapter 4 Arrays

Experiment content:

      1. The definition and assignment access of one-dimensional arrays.

      2. The definition and assignment access of a two-dimensional array.

      3. Programming realization of bubble sort.

      4. Select the programming implementation of sorting.

Experimental steps:

1. Define an integer array with 20 elements, use a random function to generate 3 digits to assign values ​​to the array and output it on the console, requiring each line to display 10 elements.

Tip: Use random function to generate 3 digits: (int)(Math.random()∗900)+100

(Math.random() returns a random number between 0 and 1, then Math.random()*900 can return a number between 0 and 900, which can be 0, but less than 900. This product is For double type, cast it to int type, and then add 100, it becomes 100, but less than 1000.)

Source code:

public class Sy4_1 {

    public static void main(String[] args){

        int[] x = new int[20];

        for(int i=0;i<x.length;i++){

           x[i] = (int)(Math.random()*900)+100;

           System.out.print(x[i]+"\t");

             if((i+1)%10==0)

             {

                 System.out.printf("\n");

             }

        }

    }

}

Screenshot of running result:

2. Use the random function to generate 16 random integers within 100 to assign values ​​to a 4×4 two-dimensional array, and the array is required to be output in rows and columns.

Source code:

public class Sy4_2 {

    public static void main(String[] args) {

        int[][] ww = new int[4][4];

        for(int i=0;i<ww.length;i++){

           for(int j=0;j<ww[i].length;j++){

               ww[i][j] = (int) (Math.random()*100);

               System.out.print(ww[i][j]+"  ");

            }

           System.out.println();

        }

    }

 

Screenshot of running result:

3. Define an integer array, which contains elements: 10, 7, 9, 2, 4, 5, 1, 3, 6, 8. Please write a program to sort the array from small to large (bubble sorting), and output each element of the array.

Tip: The idea of ​​bubbling sorting: each pass starts with the first element, compares pair by pair, and puts the larger one at the back, so that the last element is the largest one pass, and the next pass only needs to compare to n-1. Yes, after comparing n-1 times, sort the order.

Source code:

import java.sql.SQLOutput;

public class Sy4_3 {
    public static void main(String[] args) {
        int[] arr = {9,8,3,5,4,2,1,6,7};
        System.out.print("冒泡排序前:"+"\n");
        printArray(arr);
        bubbleSort(arr);
        System.out.print("冒泡排序后:"+"\n");
        printArray(arr);
    }

    public static void printArray(int [] arr) {
        //循环遍历数组的元素
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println("\n");

    }

    //定义数组的排序方法
    public static void bubbleSort(int[] arr) {
        //定义外层循环
        for(int i=0;i<arr.length;i++){
            //定义内层循环
            for(int j=0;j<arr.length-i-1;j++){
                if(arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }

            System.out.print("第"+(i+1)+"后排序为:"+"\n");
            printArray(arr);


        }

    }

 
}

Screenshot of running result:

4. Define an integer array and use a random function to generate 10 integer values ​​between 0 and 100 to assign values ​​to the array. Use selection sort to sort a one-dimensional array in ascending order, and output each element of the array.

Tip: The idea of ​​selecting and sorting is to select the smallest (or largest) element from the data elements to be sorted each time and store it at the beginning of the sequence until all the data elements to be sorted are arranged.

Source code:

 

import java.util.Random;
public class Sy4_4 {
    public static void main(String[] args) {
        int[] x = new int [10];
        Random r = new Random();
        System.out.println("随机生成的10个数字为:");
        for(int i=0;i<x.length;i++){
            x[i] = (int)(r.nextInt(100));
            System.out.print(x[i]+"\t");
        }
        for(int i = 0; i < x.length - 1; i++) {// 做第i趟排序
            int k = i;
            for(int j = k + 1; j < x.length; j++){// 选最小的记录
                if(x[j] < x[k]){
                    k = j; //记下目前找到的最小值所在的位置
                }
            }
            //在内层循环结束,也就是找到本轮循环的最小的数以后,再进行交换
            if(i != k){  //交换a[i]和a[k]
                int temp = x[i];
                x[i] = x[k];
                x[k] = temp;
            }
        }
        System.out.println();
        System.out.println("交换后:");
        for(int num:x){
            System.out.print(num+"\t");
        }
    }


}

Screenshot of running result:

 

Experiment summary

      The array provided in the Java language is used to store elements of the same type of fixed size.

      You can declare an array variable, such as numbers[100] instead of directly declaring 100 independent variables number0, number1, ..., number99.

      This experiment has a general understanding of the declaration, creation and initialization of Java arrays, and gives the corresponding codes.

  1. One-dimensional array definition

  1. Two-dimensional array definition

  1. Random number generation (Math.Random())

      Calling this Math.Random() function can return a double value with a positive sign, the value is greater than or equal to 0.0 and less than 1.0, that is, the value range is [0.0,1.0) left closed right open interval, the return value is a pseudo-random choice The number of (approximately) uniformly distributed in this range

 

Guess you like

Origin blog.csdn.net/qq_45176548/article/details/112262526