Java 数组类
java.util包中 的 Arrays 类是 Java集合框架 的一部分。这个类提供了静态的方法来动态地创建和访问 Java数组 它只由静态方法和Object类的方法组成。这个类的方法可以通过类名本身来使用。
类的层次结构如下。
java.lang.Object
↳ java.util.Arrays
极客,现在你一定想知道为什么我们需要java Arrays类,因为我们能够对数组进行声明、初始化和计算操作。这个问题的答案在于这个类的方法,我们将进一步讨论,因为实际上这些函数可以帮助程序员扩展数组的视野,例如,很多时候, 循环 被用来在数组上做一些任务,比如。
- 用一个特定的值填充一个数组。
- 对数组进行排序。
- 在数组中搜索。
- 还有更多。
这里Arrays类提供了几个静态方法,可以用来直接执行这些任务,而不需要使用循环,从而使我们的代码变得非常简短和优化。
语法: 类声明
public class Arrays
extends Object
语法: 为了使用数组
Arrays.<function name>;
Java数组类中的方法
java.util包中的Arrays类包含几个静态方法,可以用来在数组中进行填充、排序、搜索等。现在让我们来讨论一下这个类的方法,这些方法以表格的形式显示在下面。
| 方法 | 执行的操作 |
|---|---|
| asList() | 返回一个由指定数组支持的固定大小的列表。 |
| binarySearch() | 在二进制搜索算法的帮助下,在数组中搜索指定的元素 |
| binarySearch(array, fromIndex, toIndex, key, Comparator) | 使用二进制搜索算法在指定的数组范围内搜索指定的对象 |
| compare(array 1, array 2) | 对作为参数传递的两个数组进行按字母顺序的比较。 |
| copyOf(originalArray, newLength) | 复制指定的数组,截断或用默认值填充(如果需要的话),使副本具有指定的长度。 |
| copyOfRange(originalArray, fromIndex, endIndex) | 将指定数组的指定范围复制到一个新的Arrays中。 |
| deepEquals(Object[] a1, Object[] a2) | 如果两个指定的数组彼此深度相等,则返回true。 |
| deepHashCode(Object[] a) | 返回基于指定数组的 “深度内容 “的哈希代码。 |
| deepToString(Object[] a) | 返回指定数组的 “深层内容 “的字符串表示。 |
| equals(array1, array2) | 检查两个数组是否相等。 |
| fill(originalArray, fillValue) | 将此填充值分配给数组的每个索引。 |
| hashCode(originalArray) | 返回这个数组实例的整数hashCode。 |
| mismatch(array1, array2) | 查找并返回两个指定数组之间第一个不匹配的元素的索引。 |
| parallelPrefix(originalArray, fromIndex, endIndex, functionalOperator) | 用指定的函数运算符对数组的给定范围执行parallelPrefix。 |
| parallelPrefix(originalArray, operator) | 用指定的函数运算符对完整的数组执行并列前缀。 |
| parallelSetAll(originalArray, functionalGenerator) | 使用提供的生成器函数,并行地设置该数组的所有元素。 |
| parallelSort(originalArray) | 使用并行排序对指定的数组进行排序。 |
| setAll(originalArray, functionalGenerator) | 使用提供的生成器函数,设置指定数组的所有元素。 |
| sort(originalArray) | 对完整的数组按升序排序。 |
| sort(originalArray, fromIndex, endIndex) | 对指定范围的数组按升序排序。 |
| sort(T[] a, int fromIndex, int toIndex, Comparator< super T> c) | 根据指定的比较器引起的顺序对指定的对象数组的指定范围进行排序。 |
| sort(T[] a, Comparator< super T> c) | 根据指定的比较器引起的顺序对指定的对象数组进行排序。 |
| spliterator(originalArray) | 返回一个覆盖所有指定数组的Spliterator。 |
| spliterator(originalArray, fromIndex, endIndex) | 返回一个覆盖指定数组的指定范围的数组类型的Spliterator。 |
| stream(originalArray) | 返回一个以指定数组为源的顺序流。 |
| toString(originalArray) | 它返回该数组内容的字符串表示。字符串表示由一个数组元素的列表组成,用方括号(”[]”)括起来。相邻的元素用逗号和空格分开。元素通过String.valueOf()函数转换为字符串。 |
实现
例1: asList()方法
// Java Program to Demonstrate Arrays Class
// Via asList() method
// Importing Arrays utility class
// from java.util package
import java.util.Arrays;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To convert the elements as List
System.out.println("Integer Array as List: "
+ Arrays.asList(intArr));
}
}
输出
Integer Array as List: [[I@2f4d3709]
例 2: binarySearch() 方法
该方法在二进制搜索算法的帮助下搜索数组中的指定元素。
// Java Program to Demonstrate Arrays Class
// Via binarySearch() method
// Importing Arrays utility class
// from java.util package
import java.util.Arrays;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
Arrays.sort(intArr);
int intKey = 22;
// Print the key and corresponding index
System.out.println(
intKey + " found at index = "
+ Arrays.binarySearch(intArr, intKey));
}
}
输出
22 found at index = 3
例 3: binarySearch(array, fromIndex, toIndex, key, Comparator) 方法
该方法使用二进制搜索算法在指定数组的范围内搜索指定对象。
// Java program to demonstrate
// Arrays.binarySearch() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
Arrays.sort(intArr);
int intKey = 22;
System.out.println(
intKey
+ " found at index = "
+ Arrays
.binarySearch(intArr, 1, 3, intKey));
}
}
输出
22 found at index = -4
例4: 比较(数组1,数组2)方法
// Java program to demonstrate
// Arrays.compare() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// Get the second Array
int intArr1[] = { 10, 15, 22 };
// To compare both arrays
System.out.println("Integer Arrays on comparison: "
+ Arrays.compare(intArr, intArr1));
}
}
输出
Integer Arrays on comparison: 1
例5: compareUnsigned(array 1, array 2)方法
// Java program to demonstrate
// Arrays.compareUnsigned() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Arrays
int intArr[] = { 10, 20, 15, 22, 35 };
// Get the second Arrays
int intArr1[] = { 10, 15, 22 };
// To compare both arrays
System.out.println("Integer Arrays on comparison: "
+ Arrays.compareUnsigned(intArr, intArr1));
}
}
输出
Integer Arrays on comparison: 1
例6: copyOf(originalArray, newLength)方法
// Java program to demonstrate
// Arrays.copyOf() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To print the elements in one line
System.out.println("Integer Array: "
+ Arrays.toString(intArr));
System.out.println("\nNew Arrays by copyOf:\n");
System.out.println("Integer Array: "
+ Arrays.toString(
Arrays.copyOf(intArr, 10)));
}
}
输出
Integer Array: [10, 20, 15, 22, 35]
New Arrays by copyOf:
Integer Array: [10, 20, 15, 22, 35, 0, 0, 0, 0, 0]
例7: copyOfRange(originalArray, fromIndex, endIndex) 方法
// Java program to demonstrate
// Arrays.copyOfRange() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To print the elements in one line
System.out.println("Integer Array: "
+ Arrays.toString(intArr));
System.out.println("\nNew Arrays by copyOfRange:\n");
// To copy the array into an array of new length
System.out.println("Integer Array: "
+ Arrays.toString(
Arrays.copyOfRange(intArr, 1, 3)));
}
}
输出
Integer Array: [10, 20, 15, 22, 35]
New Arrays by copyOfRange:
Integer Array: [20, 15]
例8: deepEquals(Object[] a1, Object[] a2)方法
// Java program to demonstrate
// Arrays.deepEquals() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Arrays
int intArr[][] = { { 10, 20, 15, 22, 35 } };
// Get the second Arrays
int intArr1[][] = { { 10, 15, 22 } };
// To compare both arrays
System.out.println("Integer Arrays on comparison: "
+ Arrays.deepEquals(intArr, intArr1));
}
}
输出
Integer Arrays on comparison: false
例9: deepHashCode(Object[] a)方法
// Java program to demonstrate
// Arrays.deepHashCode() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[][] = { { 10, 20, 15, 22, 35 } };
// To get the dep hashCode of the arrays
System.out.println("Integer Array: "
+ Arrays.deepHashCode(intArr));
}
}
输出
Integer Array: 38475344
例10: deepToString(Object[] a)方法
// Java program to demonstrate
// Arrays.deepToString() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[][] = { { 10, 20, 15, 22, 35 } };
// To get the deep String of the arrays
System.out.println("Integer Array: "
+ Arrays.deepToString(intArr));
}
}
输出
Integer Array: [[10, 20, 15, 22, 35]]
例11: equals(array1, array2)方法
// Java program to demonstrate
// Arrays.equals() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Arrays
int intArr[] = { 10, 20, 15, 22, 35 };
// Get the second Arrays
int intArr1[] = { 10, 15, 22 };
// To compare both arrays
System.out.println("Integer Arrays on comparison: "
+ Arrays.equals(intArr, intArr1));
}
}
输出
Integer Arrays on comparison: false
例12: fill(originalArray, fillValue)方法
// Java program to demonstrate
// Arrays.fill() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Arrays
int intArr[] = { 10, 20, 15, 22, 35 };
int intKey = 22;
Arrays.fill(intArr, intKey);
// To fill the arrays
System.out.println("Integer Array on filling: "
+ Arrays.toString(intArr));
}
}
输出
Integer Array on filling: [22, 22, 22, 22, 22]
例13: hashCode(originalArray)方法
// Java program to demonstrate
// Arrays.hashCode() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To get the hashCode of the arrays
System.out.println("Integer Array: "
+ Arrays.hashCode(intArr));
}
}
输出
Integer Array: 38475313
例14: mismatch(array1, array2)方法
// Java program to demonstrate
// Arrays.mismatch() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Arrays
int intArr[] = { 10, 20, 15, 22, 35 };
// Get the second Arrays
int intArr1[] = { 10, 15, 22 };
// To compare both arrays
System.out.println("The element mismatched at index: "
+ Arrays.mismatch(intArr, intArr1));
}
}
输出
The element mismatched at index: 1
例15: parallelSort(originalArray)方法
// Java program to demonstrate
// Arrays.parallelSort() method
// Importing Arrays class from
// java.util package
import java.util.Arrays;
// Main class
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To sort the array using parallelSort
Arrays.parallelSort(intArr);
System.out.println("Integer Array: "
+ Arrays.toString(intArr));
}
}
输出
Integer Array: [10, 15, 20, 22, 35]
例16: sort(originalArray)方法
// Java program to demonstrate
// Arrays.sort() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To sort the array using normal sort-
Arrays.sort(intArr);
System.out.println("Integer Array: "
+ Arrays.toString(intArr));
}
}
输出
Integer Array: [10, 15, 20, 22, 35]
例17: sort(originalArray, fromIndex, endIndex)方法
// Java program to demonstrate
// Arrays.sort() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To sort the array using normal sort
Arrays.sort(intArr, 1, 3);
System.out.println("Integer Array: "
+ Arrays.toString(intArr));
}
}
输出
Integer Array: [10, 15, 20, 22, 35]
例18: sort(T[] a, int fromIndex, int toIndex, Comparator< super T> c) 方法
// Java program to demonstrate working of Comparator
// interface
import java.util.*;
import java.lang.*;
import java.io.*;
// A class to represent a student.
class Student {
int rollno;
String name, address;
// Constructor
public Student(int rollno, String name,
String address)
{
this.rollno = rollno;
this.name = name;
this.address = address;
}
// Used to print student details in main()
public String toString()
{
return this.rollno + " "
+ this.name + " "
+ this.address;
}
}
class Sortbyroll implements Comparator<Student> {
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
// Driver class
class Main {
public static void main(String[] args)
{
Student[] arr = { new Student(111, "bbbb", "london"),
new Student(131, "aaaa", "nyc"),
new Student(121, "cccc", "jaipur") };
System.out.println("Unsorted");
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
Arrays.sort(arr, 1, 2, new Sortbyroll());
System.out.println("\nSorted by rollno");
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
}
}
输出
Unsorted
111 bbbb london
131 aaaa nyc
121 cccc jaipur
Sorted by rollno
111 bbbb london
131 aaaa nyc
121 cccc jaipur
例19: sort(T[] a, Comparator< super T> c) 方法
// Java program to demonstrate working of Comparator
// interface
import java.util.*;
import java.lang.*;
import java.io.*;
// A class to represent a student.
class Student {
int rollno;
String name, address;
// Constructor
public Student(int rollno, String name,
String address)
{
this.rollno = rollno;
this.name = name;
this.address = address;
}
// Used to print student details in main()
public String toString()
{
return this.rollno + " "
+ this.name + " "
+ this.address;
}
}
class Sortbyroll implements Comparator<Student> {
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
// Driver class
class Main {
public static void main(String[] args)
{
Student[] arr = { new Student(111, "bbbb", "london"),
new Student(131, "aaaa", "nyc"),
new Student(121, "cccc", "jaipur") };
System.out.println("Unsorted");
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
Arrays.sort(arr, new Sortbyroll());
System.out.println("\nSorted by rollno");
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
}
}
输出
Unsorted
111 bbbb london
131 aaaa nyc
121 cccc jaipur
Sorted by rollno
111 bbbb london
121 cccc jaipur
131 aaaa nyc
例20: spliterator(originalArray)方法
// Java program to demonstrate
// Arrays.spliterator() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To sort the array using normal sort
System.out.println("Integer Array: "
+ Arrays.spliterator(intArr));
}
}
输出
Integer Array: java.util.Spliterators$IntArraySpliterator@4e50df2e
例21: splitator(originalArray, fromIndex, endIndex)方法
// Java program to demonstrate
// Arrays.spliterator() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To sort the array using normal sort
System.out.println("Integer Array: "
+ Arrays.spliterator(intArr, 1, 3));
}
}
输出
Integer Array: java.util.Spliterators$IntArraySpliterator@4e50df2e
例22: stream(originalArray)方法
// Java program to demonstrate
// Arrays.stream() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To get the Stream from the array
System.out.println("Integer Array: "
+ Arrays.stream(intArr));
}
}
输出
Integer Array: java.util.stream.IntPipeline$Head@7291c18f
例23: toString(originalArray)方法
// Java program to demonstrate
// Arrays.toString() method
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To print the elements in one line
System.out.println("Integer Array: "
+ Arrays.toString(intArr));
}
}
输出
Integer Array: [10, 20, 15, 22, 35]
极客教程