Java数组– java.util.Arrays

时间:2020-02-23 14:36:22  来源:igfitidea点击:

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

Java数组

  • Java Arrays类是Java 1.2中引入的实用程序类,用于在Java中对数组执行各种常见操作。

  • 此类是java集合框架的一部分。

  • Java Arrays类包含用于排序,搜索和比较数组的方法。

  • Arrays类还包含一个静态方法,该方法返回一个由指定数组支持的列表。

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

Java阵列范例

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

Java数组asList

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

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)

上面程序的输出是:

package com.theitroad.examples;

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

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

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);
	}
}

Java数组排序

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

[one, two, three, four, five]

上面程序的输出是:

package com.theitroad.examples;

import java.util.Arrays;

/**
 * Java Arrays Example Program
 * 
 * @author hyman
 *
 */
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+" ");
		}
	}

}

Java数组binarySearch

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

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

上面程序的输出是:

package com.theitroad.examples;

import java.util.Arrays;

/**
 * Java Arrays binarySearch
 * 
 * @author hyman
 *
 */
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");
		}

	}

}

Java数组等于

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

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

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

上面程序的输出如下。

package com.theitroad.examples;

import java.util.Arrays;

/**
 * Java Arrays Example Program
 * 
 * @author hyman
 *
 */
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);
		}

	}

}

比较嵌套数组

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

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

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

上面程序的输出是:

package com.theitroad.examples;

import java.util.Arrays;

/**
 * Java Arrays Example Program
 * 
 * @author hyman
 *
 */
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);
		}

	}

}

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

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

Arrays b1 and b2 are not equal with Result : false

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

package com.theitroad.examples;

import java.util.Arrays;

/**
 * Java Arrays Example Program
 * 
 * @author hyman
 *
 */
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);
		}

	}

}