close

This chapter introduces the basic concept of copying an array, a two-dimensional array and keywords for arrays. I believe you must have such a question when programming arrays. How to merge a array and b array into c array? What is the connection between two-dimensional array and one-dimensional array? You should understand these concepts after reading this tutorial.

Copy an Array

Copy the value of one array to another array

The specific code is as follows:

System.arraycopy(src, srcPos, dest, destPos, length)

src: source array

srcPos: the starting position of the data copied from the source array

dest: destination array

destPos: Copy to the start position of the target array

length: the length of the copy

The specific code is as follows:

public class array {

            public static void main(String[] args) {

        int a [] = new int[]{11,24,38,89,55,9};

        int b[] = new int[5];//A space of length 3 is allocated, but no value is assigned

        //Assign the first 5 bits of the a array to the b array through array assignment

        //Method one: for loop

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

                                    b[i] = a[i];

                        }

        ///Method two: System.arraycopy(src, srcPos, dest, destPos, length)

        //src: Source array

        //srcPos: The starting position of the data copied from the source array

        //dest: Target array

        //destPos: Copy to the start position of the target array

        //length: Copy length     

        System.arraycopy(a, 0, b, 0, 5);

        //Print out the content

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

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

        }

    }

}

  • Exercise 1-Combine Arrays

First prepare two arrays, their length is a random number between 10-20, and initialize the two arrays with random numbers.

Then prepare the third array, the length of the third array is the sum of the first two

Merge the first two arrays into the third array through System.arraycopy

The specific code is as follows:

public class array {

    public static void main(String[] args) {

        int a[] = new int[(int) (Math.random() * 10)+10];

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

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

        int b[] = new int[(int) (Math.random() * 10)+10];

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

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

        System.out.println(“Contents of array a:”);

        for (int i : a) {

                                    System.out.print(i+” “);

                        }

        System.out.println();

        System.out.println(“Contents of array b:”);

        for (int i : b) {

             System.out.print(i+” “);

        }

        System.out.println();

        int c[] = new int[a.length+b.length];

        System.arraycopy(a, 0, c, 0, a.length);

        System.arraycopy(b, 0, c, a.length, b.length);

        System.out.println(“Contents of array c:”);

        for (int i : c) {

             System.out.print(i+” “);

        }

    }

}

The final output of the console:

  • Two-dimensional array

Firstly, we have to clarify the definition of a one-bit array.

One-dimensional array, each element in it is a basic type int.

The specific code is as follows:

int a [] =new int [] {1,2,3,4,5};

A two-dimensional array is based on a one-dimensional array, and each element in it is a one-dimensional array.

So, a two-dimensional array is also called an array of arrays.

The specific code is as follows:

int b [] [] = new int [] [] {

   {1,2,3},

   {4,5,6},

   {7,8,9}

};

  • Initialize a two-dimensional array

The specific code is as follows:

public class array {

            public static void main(String[] args) {

                           //Initialize a two-dimensional array

                           int[][] a = new int[2][3]; //There are two one-dimensional arrays, and the length of each one-dimensional array is 3

                           a[1][2] = 3;  //One-dimensional array can be accessed directly because space has been allocated

                           //Only a two-dimensional array is allocated

                           int[][] b = new int[2][]; //There are two one-dimensional arrays, and the length of each one-dimensional array has not been allocated yet

                           b[0]  =new int[3]; //The length must be allocated in advance before access

                           b[0][2] = 5;

                           //Allocate space while specifying content

                           int[][] c = new int[][]{

                                                   {1,2,4},

                                                   {4,5},

                                                   {6,7,8,9}

                           };

                }

            }

  • Exercise 2-two-dimensional array

Define a 5X5 two-dimensional array. Then fill the two-dimensional array with random numbers.

Find the largest value in this two-dimensional array and print out its two-dimensional coordinates.

The specific code is as follows:

public class array {

            public static void main(String[] args) {

                        int a[][] = new int[5][5];

                        // Initialize this array

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

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

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

                                    }

                        }

                        // Print the contents of this array:

                        for (int[] row : a) {

                                    for (int each : row) {

                                                System.out.print(each + “\t”);

                                    }

                                    System.out.println();

                        }

                        int max = -1;

                        // Maximum coordinate

                        int target_i = -1;

                        int target_j = -1;

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

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

                                                if (a[i][j] > max) {

                                                            max = a[i][j];

                                                            target_i = i;

                                                            target_j = j;

                                                }

                                    }

                        }

                        System.out.println(“The biggest one is:” + max);

                        System.out.println(“Its coordinates are[” + target_i + “][” + target_j + “]”);

            }

}

The final output of the console:

Arrays

Arrays is a tool class for arrays, which can perform functions such as sorting, searching, copying and filling. Greatly improve the work efficiency of developers.

The keyword application part of Arrays mainly has the following modules:

Copy of Range, to String (), sort, binary, Search, equals, fill. These keywords can realize the array assignment, convert the array to string, sort the array, search the array, judge whether two arrays are the same, and fill the array.

Using the method of combining arrays, the a and b arrays can be combined into a c array. After analyzing the concept of a one-dimensional array, a two-dimensional array can be constructed by combining multiple one-bit arrays.

Tags : combined arrayone-dimensional arraytwo-dimensional array

Leave a Response