Introduction to Java -- array

Keywords: Java jar

catalogue

1. Array

1.1 concept

1.2 creation of array

  1.3 array creation process

1.4 length of array

1.5 array variables

2. Tools

2.1 Arrays

1.Arrays.toString

2.Arrays.sort

3.Arrays.copyOf

1. Array

1.1 concept

Array, marked with "[]", is used to store the aggregation of multiple data of the same type. If you want to obtain the element values in the array, you can obtain them through subscripts.

The array subscript starts from "0", and the maximum subscript is the array length - 1.

  The length of int[] a is 5, that is, there are five element values in it. The subscript starts with "0" and the element value of subscript [1] is "2"

1.2 creation of array

There are two common ways to create arrays:

1. Static initial value

int[]a = {1,2,3};
int[]b = new int[]{1,2,3,4,5,6};
//Creates an array length and assigns a value to the array

2. Dynamic initial value

int[]a = new int[5];//Dynamic initial value, giving the array length, in which the elements are assigned by themselves

  1.3 array creation process

Take: int[]a = new int[5], for example

        1. Open up a continuous space in the memory to store data, with a length of 5

        2. Complete the initialization process for the data, give each element a default value, and the default value of int is "0".

        3. After the initialization of the array, a unique address value will be assigned.

        4. Give the unique address value to variable a to save

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, and the length of the array is allowed to be 0.

1.5 array variables

Traversal: access each position of the array from the beginning to the end of the array to obtain the elements of each position. For example:

public class Test {

	public static void main(String[] args) {
		//Randomly create an array with a length of 5 and print the array
		int[]a =new int[5];
        //Traverse the array and assign values to each element on the array
		for (int i = 0; i < a.length; i++) {
            //Use Random() to add a random number. nextInt() indicates that the range value of the random number is [0100]
			a[i]= new Random().nextInt(100);
		}
        //To print an array, you can print the array directly except for char. Other types of direct printing are the address values of the array. You need to use Arrays.toString() to print the element values in the array
		System.out.println(a);//[I@4c6e276e
		System.out.println(Arrays.toString(a));//[55, 72, 24, 27, 89]

	}
}

2. Tools

2.1 Arrays

1.Arrays.toString

Link the data in the array into a string [data 1, data 2,......]

2.Arrays.sort

Sort the data in the array. For the basic type of data, the optimized quick sorting algorithm is used, which has high efficiency

The optimized merge and arrange algorithm is used for reference type data

3.Arrays.copyOf

Assigns an array to a new array of the specified length

If the length of the new array is greater than the original array, copy the original array and then increase the length later

If the length of the new array is less than the original array, part of the data will be intercepted

Arrays.copyOfRange() is used to intercept the specified element

2.2 Random

Assign random values to the data, while nextInt limits the range of assignment to [0100)

public class Test {

	public static void main(String[] args) {
		//Randomly create an array with a length of 5 and print the array
		int[]a =new int[5];
		for (int i = 0; i < a.length; i++) {
			a[i]= new Random().nextInt(100);
		}
		System.out.println(Arrays.toString(a));//Print the data of the original array
		int[] b= Arrays.copyOf(a,10);//Specify new length, because original array length is 5, and subsequent 5 data are increased with the no value (default value is 0)
		System.out.println(Arrays.toString(b));//Print data for new array
		int[] c=Arrays.copyOf(a,3);//Specify a new length, smaller than the original array, and intercept the first three data
		System.out.println(Arrays.toString(c));//Print array c
		int[] d=Arrays.copyOfRange(a, 1, 4);//Specify where to intercept and where to end, retain the header data and remove the tail data
		System.out.println(Arrays.toString(d));//Print data d
		/**
		 *Array a: [89, 10, 86, 46, 75]
		 *Array b: [89, 10, 86, 46, 75, 0, 0, 0, 0, 0]
		 *Array c: [89, 10, 86]
		 *Array d:[10, 86, 46]
		 * */
	}
}

Posted by Dude0 on Sat, 20 Nov 2021 22:22:51 -0800