Java's array tool Arrays

The common methods of Arrays tools are briefly described as follows:

  • Arrays.equals(a1,a2): Determine whether the two arrays a1 and a2 are equal, that is, whether each element is equal;

  • Arrays.fill(a,val): fill the specified value val into the array a;

  • dest = Arrays.copyOf(src,newLength): Assign the contents of the array src to the array dest, and the length of dest is newLength;

  • Arrays.sort(a): Sort the internal elements of array a, sorting in ascending order by default.

    To use the above method, you need to import the array tool import java.util.Arrays;

1.Arrays.equals方法
2.Arrays.fill method

For example, there are now 10 array variables of 99. It is obviously a burden to input repeated numbers, especially when there are many repeated numbers. Therefore, in order to solve this problem, the Arrays.fill method is used to optimize the following code:

		......
		// 构造一个包含十个99的数组变量
		// int[] prices = {99, 99, 99, 99, 99, 99, 99, 99, 99, 99};
		int[] prices = new int[10]; // 声明一个整型数组,数组大小为10
		Arrays.fill(prices, 99); // 给整型数组的每个元素全部填写99
		for (int price : prices) {
    
     // 循环遍历并打印整型数组的所有元素数值
			System.out.println("price = "+price);
3.Arrays.copyOf method

This method allows copying several (indeterminate) elements from the source array to the target array.

			......
			int[] pricesOrigin = {
    
     99, 99, 99, 99, 99 }; // 声明一个整型数组,数组大小为5,且5个元素全为99
			for (int price : pricesOrigin) {
    
    
				System.out.println("origin price = " + price);
			}
			// 复制数组的第三个办法:调用Arrays工具的copyOf方法。允许复制部分元素
			int[] pricesCopy = Arrays.copyOf(pricesOrigin, pricesOrigin.length);
			for (int price : pricesCopy) {
    
     // 循环遍历并打印整型数组的所有元素数值
				System.out.println("copy price = " + price);
			}

			// 改变copyOf方法的第二个参数值,允许复制指定大小的数组元素
			int[] pricesPart = Arrays.copyOf(pricesOrigin, pricesOrigin.length - 1);
			for (int price : pricesPart) {
    
     // 循环遍历并打印整型数组的所有元素数值
				System.out.println("part price = " + price);
			}

			// 把copyOf方法的返回值赋给原数组,可以动态调整该数组的大小
			pricesOrigin = Arrays.copyOf(pricesOrigin, pricesOrigin.length + 1);
			for (int price : pricesOrigin) {
    
     // 循环遍历并打印整型数组的所有元素数值
				System.out.println("origin price = " + price);
			}
4.Arrays.sort method

As the name implies, the sort method of the Arrays tool sorts the array elements, and the default sort result is ascending.

The following code is to generate a dynamic random array, involving arrays, loops, colon jumps and other techniques for sorting:

			......
			int[] numbers = new int[10]; // 创建一个大小为10的动态整型数组
			loop: for (int i = 0; i < numbers.length; i++) {
    
    
				int item = new Random().nextInt(100); // 生成一个小于100的随机整数
				// 下面的循环用来检查数组中是否已经存在该随机数
				for (int j = 0; j < i; j++) {
    
    
					if (numbers[j] == item) {
    
     // 已经存在该随机数,则继续第一层循环,重新生成随机数
						i--; // 本次循环做了无用功,取消当前的计数
						continue loop; // 继续以loop标记的外层循环
					}
				}
				numbers[i] = item; // 原数组不存在该随机数,则把随机数加入到数组中
			}
			// 对整型数组numbers里的元素排序,sort方法得到的结果是升序排列
			Arrays.sort(numbers);
			for (int number : numbers) {
    
     // 循环遍历并打印整型数组的所有随机数
				System.out.println("number = " + number);
			}

The sort method optimization code is behind

Guess you like

Origin blog.csdn.net/weixin_46312449/article/details/110481624