Java数组– java.util.Arrays

Java Arrays class consists exclusively of static methods that operates on array.

Java Arrays类仅由对数组进行操作的静态方法组成。

Java数组 (Java Arrays)

  1. Java Arrays class was introduced in Java 1.2 as a utility class to perform various common operations on arrays in java.

    Java Arrays类是Java 1.2中引入的实用程序类,用于在Java中对数组执行各种常见操作。
  2. This class is part of java collections framework.

    此类是java collections框架的一部分。
  3. Java Arrays class contains methods for sorting, searching and comparing arrays.

    Java Arrays类包含用于排序,搜索和比较数组的方法。
  4. Arrays class also contains a static method that returns a list backed by the specified array.

    Arrays类还包含一个静态方法,该方法返回一个由指定数组支持的列表。
  5. Arrays class contains overloaded methods that support primitive data types. These methods are:
    public static <T> List<T> asList(T… a)
    public static void sort(int[] a)
    public static int binarySearch(int[] a, int k)
    public static boolean equals(int[] a, int[] a2)

    Arrays类包含支持原始数据类型的重载方法。 这些方法是:

Java阵列范例 (Java Arrays Examples)

Let’s have a look at the Arrays methods example through some programs.

让我们通过一些程序看一下Arrays方法示例。

Java数组asList (Java Arrays asList)

Java Arrays provides asList method that returns a list backed by the specified array. Below is a simple program for showing array as a List.

Java数组提供了asList方法,该方法返回由指定数组支持的列表 。 下面是一个简单的程序,用于将数组显示为列表。

package com.journaldev.examples;

import java.util.Arrays;
import java.util.List;

/**
 * Java Arrays Example Program
 * 
 * @author pankaj
 *
 */

public class ArraysAsListExample {
	
	public static void main(String[] args) {
		
		String[] strings = {"one", "two", "three", "four", "five"};
		// strings array is converted into a List
		List<String> list = Arrays.asList(strings);
		System.out.println(list);
	}
}

Output of above program is:

上面程序的输出是:

[one, two, three, four, five]

Java数组排序 (Java Arrays sort)

Java Arrays provides sort method that sorts the element of specified array and also sorts the specified range of the given array into ascending order.
Below is a simple program for sorting arrays in java.

Java Arrays提供了sort方法,该方法可以对指定数组的元素进行排序,还可以将给定数组的指定范围按升序排序。
下面是一个用于在Java中对数组进行排序的简单程序。

package com.journaldev.examples;

import java.util.Arrays;

/**
 * Java Arrays Example Program
 * 
 * @author pankaj
 *
 */
public class ArraysSortExample {

	public static void main(String[] args) {
		
		// Sorting Character
		char[] chars = {'B', 'D', 'C', 'A', 'E'};
		Arrays.sort(chars);
		System.out.print("Sorted Characters : ");
		for (char character : chars) {
			System.out.print(character+" ");
		}
		
		// Sorting Integer
		int[] integers = {5, 2, 1, 4, 3};
		Arrays.sort(integers);
		System.out.print("\nSorted Integers : ");
		for (int i : integers) {
			System.out.print(i+" ");
		}
		
		// Sorting Specific Range of Integers
		int[] ints = {5, 2, 1, 4, 3, 9, 6, 8, 7, 10};
		int fromIndex = 2;
		int toIndex = 7;
		Arrays.sort(ints, fromIndex, toIndex);
		System.out.print("\nSorted Integers of Specific Range : ");
		for (int i : ints) {
			System.out.print(i+" ");
		}
	}

}

Output of above program is:

上面程序的输出是:

Sorted Characters : A B C D E 
Sorted Integers : 1 2 3 4 5 
Sorted Integers of Specific Range : 5 2 1 3 4 6 9 8 7 10

Java数组binarySearch (Java Arrays binarySearch)

Java Arrays binarySearch method uses binary search algorithm to search specified value from the elements of specified array and also searches from the specified range of the given array.

Java Arrays BinarySearch方法使用二进制搜索算法从指定数组的元素中搜索指定值,并从给定数组的指定范围中进行搜索。

package com.journaldev.examples;

import java.util.Arrays;

/**
 * Java Arrays binarySearch
 * 
 * @author pankaj
 *
 */
public class ArraysBinarySearchExample {

	public static void main(String[] args) {

		// Searching a value from array of integer
		int[] integers = { 5, 2, 1, 4, 3, 9, 6, 8, 7, 10 };
		int index = Arrays.binarySearch(integers, 2);
		if (index >= 0) {
			System.out.println("Element is found at the index :" + index);
		} else {
			System.out.println("Element is not found");
		}

		// Searching a value from array of integer with specific range
		int fromIndex = 2;
		int toIndex = 7;
		int index2 = Arrays.binarySearch(integers, fromIndex, toIndex, 9);
		if (index2 >= 0) {
			System.out.println("Element is found at the index :" + index2);
		} else {
			System.out.println("Element is not found");
		}

	}

}

Output of above program is:

上面程序的输出是:

Element is found at the index :1
Element is found at the index :5

Java数组等于 (Java Arrays equals)

Java Arrays provides equals method that is used to compare two arrays of the same type and returns boolean result. It returns true if two given arrays are equal to one another.

Java数组提供了equals方法,该方法用于比较两个相同类型的数组并返回布尔结果。 如果两个给定的数组彼此相等,则返回true。

Below is a simple program to compare arrays.

下面是一个比较数组的简单程序。

package com.journaldev.examples;

import java.util.Arrays;

/**
 * Java Arrays Example Program
 * 
 * @author pankaj
 *
 */
public class ArraysEqualExample {

	public static void main(String[] args) {

		// Compare two arrays of type integer which are equal
		int[] a1 = { 1, 2, 3 };
		int[] a2 = { 1, 2, 3 };

		boolean equal = Arrays.equals(a1, a2);
		if (equal) {
			System.out.println("Arrays a1 and a2 are equal with Result : " + equal);	
		}else {
			System.out.println("Arrays a1 and a2 are not equal with Result : " + equal);
		}
		

		// Compare two arrays of type integer which are not equal
		int[] b1 = { 1, 2, 3 };
		int[] b2 = { 4, 5, 6 };

		boolean equal1 = Arrays.equals(b1, b2);
		


if (equal1) {
			System.out.println("Arrays b1 and b2 are equal with Result : " + equal1);	

		}else {
			System.out.println("Arrays b1 and b2 are not equal with Result : " + equal1);
		}

	}

}

Output of above program is given below.

上面程序的输出如下。

Arrays a1 and a2 are equal with Result : true
Arrays b1 and b2 are not equal with Result : false

比较嵌套数组 (Compare Nested Arrays)

What if the array is inside of another array? Will Arrays equal method is capable to compare nested arrays?

如果该数组在另一个数组内部怎么办? 数组相等方法是否可以比较嵌套数组?

Let’s have a look at the below example program.

让我们看一下下面的示例程序。

package com.journaldev.examples;

import java.util.Arrays;

/**
 * Java Arrays Example Program
 * 
 * @author pankaj
 *
 */
public class ArraysNestedArrayExample {

	public static void main(String[] args) {
		// Compare two nested arrays of type integer which are equal
		int[] a1 = { 1, 2, 3 };
		int[] a2 = { 1, 2, 3 };
		Object[] b1 = {a1};
		Object[] b2 = {a2};
		boolean equal = Arrays.equals(b1, b2);
		
if (equal) {
			System.out.println("Arrays b1 and b2 are equal with Result : " + equal);
		} else {
			System.out.println("Arrays b1 and b2 are not equal with Result : " + equal);
		}

	}

}

Output of above program is:

上面程序的输出是:

Arrays b1 and b2 are not equal with Result : false

We can see that the output of the program is false despite the two arrays are equal and contains same number of elements.
Actually equal method is not able to compare nested arrays, for that Arrays have another method called deepEquals.

我们可以看到,尽管两个数组相等并且包含相同数量的元素,但是程序的输出为false。
实际上equal方法不能比较嵌套数组,因为该数组还有另一个称为deepEquals的方法。

Let’s have a look at the below example program.

让我们看一下下面的示例程序。

package com.journaldev.examples;

import java.util.Arrays;

/**
 * Java Arrays Example Program
 * 
 * @author pankaj
 *
 */
public class ArraysNestedArrayExample {

	public static void main(String[] args) {
		// Compare two nested arrays of type integer which are equal
		int[] a1 = { 1, 2, 3 };
		int[] a2 = { 1, 2, 3 };
		Object[] b1 = {a1};
		Object[] b2 = {a2};
		boolean equal = Arrays.deepEquals(b1, b2);
		
if (equal) {
			System.out.println("Arrays b1 and b2 are equal with Result : " + equal);
		} else {
			System.out.println("Arrays b1 and b2 are not equal with Result : " + equal);
		}

	}

}

Output of above arrays equals program is:

上述数组等于程序的输出为:

Arrays b1 and b2 are equal with Result : true

That’s all for java Arrays class, I hope nothing important got missed here.

Java Arrays类就这些了,我希望这里没有重要的事情。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/16770/java-arrays-java-util-arrays

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值