📜  Java中的数组类

📅  最后修改于: 2022-05-13 01:55:15.048000             🧑  作者: Mango

Java中的数组类

Java.util 包中的Arrays类是Java Collection Framework的一部分。此类提供静态方法来动态创建和访问Java数组。它仅由静态方法和 Object 类的方法组成。这个类的方法可以被类名本身使用。

类层次结构如下:

java.lang.Object
 ↳ java.util.Arrays

极客,现在您一定想知道为什么我们能够在数组上声明、初始化和计算操作时需要Java Arrays 类。虽然这个问题的答案在于我们将进一步讨论的这个类的方法,因为实际上这些函数可以帮助程序员扩展数组的视野,例如,经常使用循环在数组上执行一些任务,例如:

  • 用特定值填充数组。
  • 对数组进行排序。
  • 在数组中搜索。
  • 还有很多。

语法:类声明

public class Arrays
    extends Object

语法:为了使用数组

Arrays.;

Java数组类中的方法

Java.util 包的 Arrays 类包含几个静态方法,可用于在数组中填充、排序、搜索等。现在让我们讨论这个类的方法,下面以表格的形式显示如下:

Methods Action Performed
asList()Returns a fixed-size list backed by the specified Arrays
binarySearch()Searches for the specified element in the array with the help of the Binary Search Algorithm
binarySearch(array, fromIndex, toIndex, key, Comparator)Searches a range of the specified array for the specified object using the Binary Search Algorithm
compare(array 1, array 2)Compares two arrays passed as parameters lexicographically.
copyOf(originalArray, newLength)Copies the specified array, truncating or padding with the default value (if necessary) so the copy has the specified length.
copyOfRange(originalArray, fromIndex, endIndex)Copies the specified range of the specified array into a new Arrays.
deepEquals(Object[] a1, Object[] a2)Returns true if the two specified arrays are deeply equal to one another.
deepHashCode(Object[] a) Returns a hash code based on the “deep contents” of the specified Arrays.
deepToString(Object[] a)Returns a string representation of the “deep contents” of the specified Arrays.
equals(array1, array2)Checks if both the arrays are equal or not.
fill(originalArray, fillValue)Assigns this fill value to each index of this arrays.
hashCode(originalArray) Returns an integer hashCode of this array instance.
mismatch(array1, array2) Finds and returns the index of the first unmatched element between the two specified arrays.
parallelPrefix(originalArray, fromIndex, endIndex, functionalOperator)Performs parallelPrefix for the given range of the array with the specified functional operator.
parallelPrefix(originalArray, operator)Performs parallelPrefix for complete array with the specified functional operator. 
parallelSetAll(originalArray, functionalGenerator)Sets all the elements of this array in parallel, using the provided generator function. 
parallelSort(originalArray)Sorts the specified array using parallel sort.
setAll(originalArray, functionalGenerator)Sets all the elements of the specified array using the generator function provided. 
sort(originalArray)Sorts the complete array in ascending order. 
sort(originalArray, fromIndex, endIndex)Sorts the specified range of array in ascending order.
sort(T[] a, int fromIndex, int toIndex, Comparator< super T> c)Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.
sort(T[] a, Comparator< super T> c)Sorts the specified array of objects according to the order induced by the specified comparator.
spliterator(originalArray)Returns a Spliterator covering all of the specified Arrays.
spliterator(originalArray, fromIndex, endIndex) Returns a Spliterator of the type of the array covering the specified range of the specified arrays.
stream(originalArray) Returns a sequential stream with the specified array as its source.
toString(originalArray) It returns a string representation of the contents of this array. The string representation consists of a list of the array’s elements, enclosed in square brackets (“[]”). Adjacent elements are separated by the characters a comma followed by a space. Elements are converted to strings as by String.valueOf() function.

执行:

示例 1: asList() 方法

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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


Java
// 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 {
    // 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]);
    }
}


Java
// 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 {
 
    // 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]);
    }
}


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


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


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


Java
// 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 as List: [[I@2f4d3709]

示例 2: binarySearch() 方法

此方法借助二分搜索算法搜索数组中的指定元素。

Java

// 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

// 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: compare(array 1, array 2) 方法

Java

// 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

// 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

// 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

// 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

// 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

// 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

// 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

// 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

// 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

// 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

// 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

// 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

// 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

// 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

// 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 {
    // 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

// 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 {
 
    // 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

// 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: spliterator(originalArray, fromIndex, endIndex) 方法

Java

// 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

// 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

// 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]