Java Arrays basic tools

The Arrays class is a utility class, which contains many methods of array operations.

All methods in this Arrays class are static modified methods (static modified methods can be called directly by the class name), and the methods can be called directly in the form of Arrays.xxx(xxx).

int binarySearch(type[] a, type key)

Use the dichotomy to query the index where the key element value appears in the a array . If the a array does not contain the key element value, a negative number is returned . When calling this method, the elements in the array must be arranged in ascending order so that the correct result can be obtained.

int binarySearch(type[] a, int fromIndex, int toIndex, type key)

This method is similar to the previous method, but it only searches for elements indexed from fromIndex to toIndex in the a array. When calling this method, the elements in the array must be arranged in ascending order so that the correct result can be obtained.

type[] copyOf(type[] original, int length)

This method will copy the original array into a new array, where length is the length of the new array. If length is less than the length of the original array, the new array is the first length elements of the original array. If length is greater than the length of the original array, the front element of the new array is all the elements of the original array, followed by 0 (numerical type), false (boolean type) or null (reference type).

type[] copyOfRange(type[] original, int from, int to)

This method is similar to the previous method, but this method only copies the elements from the from index to the to index of the original array.

boolean equals(type[] a, type[] a2)

If the lengths of the a array and the a2 array are the same, and the array elements of the a array and the a2 array are also the same, this method will return true.

void fill(type[] a, type val)

This method will assign all elements of the a array to val.

void fill(type[] a, int fromIndex, int toIndex, type val)

This method has the same effect as the previous method, except that this method only assigns the array elements indexed from fromIndex to toIndex of the a array to val.

void sort(type[] a)

This method sorts the array elements of the a array.

void sort(type[] a, int fromIndex, int toIndex)

This method is similar to the previous method, except that this method only sorts the elements indexed from fromIndex to toIndex.

String toString(type[] a)

This method converts an array into a string. This method concatenates multiple array elements in sequence, and multiple array elements ,are separated by commas and spaces.

The Arrays class is in the java.util package. In order to use the Arrays class in the program, the java.util.Arrays class must be imported into the program.

import java.util.Arrays;
import java.util.Scanner;
class test10 
{
	public static void main(String[] args) 
	{
		//定义一个数组
		int[] a = new int[]{3,4,5,6};
		//定义另一个数组
		int[] b = new int[]{3,4,5,6};

		//判断数组是否相等
		System.out.println("a数组和b数组是否相等:" + Arrays.equals(a,b));
		//通过复制a生成一个新的数组
		int[] c = Arrays.copyOf(a,6);
		System.out.println("a数组和c数组是否相等:" + Arrays.equals(a,c));  //false
		//输出c数组中的元素
		System.out.println("c数组的元素为:" + Arrays.toString(c));  //[3, 4, 5, 6, 0, 0]
		//将c数组的第三个元素(包括)到第五个元素(不包括)赋值为1
		Arrays.fill(c, 2, 4, 1);
		System.out.println("c数组的元素为:" + Arrays.toString(c));
		int[] d = new int[]{3,8,0,1,60,45,78,100,23};
		//对d数组进行排序  升序
		Arrays.sort(d);
		System.out.println("c数组的元素为:" + Arrays.toString(d));
		
		Scanner input = new Scanner(System.in);
		int n;
		System.out.print("请输入要查找的值:");
		if(input.hasNextInt()){
			n = input.nextInt();  //输入要查找的值
			int index = Arrays.binarySearch(d, n);
			if(index > 0){
				System.out.println("" + d[index] + "的下标为:" + index);
			}
			else{
				System.out.println("查无此元素");
			}
		}
		else{
			System.out.println("请输入正确的值(int)");
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_43629083/article/details/108631744