close

This chapter introduces the basic concept of creating an array and initializing an array. By learning the basic concepts of creating an array and initializing an array, you can finally complete the programming of realizing the minimum value of the array and inverting the array.

  1. The array

The array is a fixed-length container containing the same type of data.

  • Declare array

int [] a/int a []: declares an array variable.

[] indicates that the variable is an array.

int means that each element in the array is an integer.

a is the variable name.

  • Create array

When creating an array, specify the length of the array.

Reference concept:

If the variable represents an array, such as a, we call a reference.

Different from basic types.

int c = 5; This is called assigning c to 5.

Declare a reference int [] a;

a = new int [8];

Let “a” the reference point to the array.

The specific code is as follows:

public class array {

            public static void main(String[] args) {

        //Declare a reference

        int a[];

        //Create an array of length 8, and use the reference a to point to the array

        a = new int[8];

        int b[] = new int[8]; //While declaring, point to an array

    }

}

  • Access array

Array subscript base 0.

Subscript 0, which represents the first number in the array.

The specific code is as follows:

public class array {

                public static void main(String[] args) {

                    int[] a;

                    a = new int[8];

                    a[0]= 1;  //Subscript 0, which represents the first number in the array

                    a[1]= 2;

                    a[2]= 3;

                    a[3]= 4;

                    a[4]= 5;

                }

            }

  • Array length

The. length property is used to access the length of an array.

The array access index range is 0 to length -1.

Once this range is exceeded, an out-of-bounds exception of the array subscript will occur.

The specific code is as follows:

public class array {

            public static void main(String[] args) {

                        int[] a;

                        a = new int[5];

                        System.out.println(a.length); // Print the length of the array

                        a[4]=100; //Subscript 4, essentially the “5th”, which is the last.

                        a[5]=101; //Subscript 5, which is essentially the “sixth”, out of range, an out-of-bounds array subscript exception occurs.

            }

}

  • Exercise 1-Array Minimum

First create an array of length 5.

Then assign random integers to each bit of the array.

Through the for loop, traverse the array to find the smallest value.

There are many ways to obtain random integers ranging from 0 to 100. The following is one of the reference methods:

(int) (Math. random () * 100)

The specific code is as follows:

public class array {

            public static void main(String[] args) {

                                    int[] a = new int[4];

                                    a[0] = (int) (Math.random() * 100);

                                    a[1] = (int) (Math.random() * 100);

                                    a[2] = (int) (Math.random() * 100);

                                    a[3] = (int) (Math.random() * 100);

                                    System.out.println(“The random numbers in the array are:”);

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

                        System.out.print(a[i] + ” “);

                    System.out.println();

                    int min = 100;

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

                        if(  a[i] < min )

                            min = a[i];

                    }

                    System.out.println(“The smallest value found is:” +min);

                }

}

The final output of the console:

  1. Initialize the array
  2. Allocate space and assign value step by step

The specific code is as follows:

public class array {

            public static void main(String[] args) {

                                    int a[] = new int[8]; //An array of length 8 is allocated, but no value is assigned

                                    //No assignment, then the default value will be used

                                    //As an array of Integer type, the default value is 0

                                    System.out.println(a[0]);

                                    //Assignment

                                    a[0] = 100;

                                    a[1] = 101;

                                    a[2] = 103;

                                    a[3] = 120;

                                    a[4] = 140;

                                    a[5] = 200;

                                    a[6] = 250;

                                    a[7] = 300;

                        }

            }

  • Allocate space and assign values at the same time

The specific code is as follows:

public class array {

            public static void main(String[] args) {

                        //Method 1: Allocate space and assign values

                        int[] a = new int[]{100,102,444,836,3236};

                        //Method 2: Omit new int[], the effect is the same

                        int[] b = {100,102,444,836,3236};

                        //Method 3: Allocate space and specify content at the same time

                        //In this example, the length of the array definition is 3, and the length of the content is 5, which creates a contradiction.

                        //So if you specify the contents of the array, you cannot set the length of the array at the same time

                        int[] c = new int[3]{100,102,444,836,3236};

            }

}

  • Exercise 2-Array inversion

At first, create an array of length 5 and fill it with random numbers.

Use for loop or while loop to achieve the reverse effect on this array.

The specific code is as follows:

public class array {

            public static void main(String[] args) {

                        int a[]=new int[5];

        a[0] = (int) (Math.random() * 100);

        a[1] = (int) (Math.random() * 100);

        a[2] = (int) (Math.random() * 100);

        a[3] = (int) (Math.random() * 100);

        a[4] = (int) (Math.random() * 100);

        System.out.println(“The values in the array are:”);

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

            System.out.print(a[i] + ” “);

        /*Method 1: Use a temporary array*/

        System.out.println();

        //Prepare temporary array

        int[] temp = new int[a.length];

        //Copy the contents of the original array to the temporary array

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

            temp[i] = a[i];

        }

        System.out.println(“The individual values in the temporary array are:”);

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

            System.out.print(temp[i] + ” “);

        System.out.println();

        //The reverse method is to put the data of the temporary array one by one into the original array

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

            a[i] = temp[temp.length-1-i];

        }

        System.out.println(“The values in the inverted array are:”);

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

            System.out.print(a[i] + ” “);

        System.out.println();

        /*Method 2: Perform a head-to-tail swap*/      

        for (int i = 0; i < a.length/2; i++) {

             int middle = a[a.length-i-1];

             a[a.length-i-1] = a[i];

             a[i] = middle;

                        }       

        System.out.println(“The values in the array after being reversed again are:”);

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

                                    System.out.print(a[i] + ” “);

                        }

        System.out.println(); 

            }

}

The final output of the console:

Today’s tutorial is over, I hope people who watch this tutorial can do it by themselves and type the code line by line. By studying the concepts of creating an array and initializing an array, you can design the programming of realizing the minimum value of the array and inverting the array.

Tags : Program The Minimum And Inverted Array

Leave a Response