当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Arrays用法及代码示例


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

类层次结构如下:

java.lang.Object
 ? java.util.Arrays

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

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

Here Arrays class provides several static methods that can be used to perform these tasks directly without the use of loops, hence forth making our code super short and optimized.

用法:类声明

public class Arrays
    extends Object

用法:为了使用数组

Arrays.<function name>;

Java 数组类中的方法

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

方法 执行的操作
asList() 返回由指定数组支持的固定大小列表
binarySearch() 借助二分查找算法在数组中搜索指定元素
binarySearch(array, fromIndex, toIndex, key, Comparator) 使用二分查找算法在指定数组的范围内搜索指定对象
比较(数组1,数组2) 按字典顺序比较作为参数传递的两个数组。
Arrays 复制指定的数组,使用默认值截断或填充(如果需要),以便副本具有指定的长度。
Arrays 将指定数组的指定范围复制到新数组中。
Arrays 如果两个指定的数组彼此深度相等,则返回 true。
deepHashCode(对象[] a) 返回基于指定数组的“deep contents”的哈希码。
Arrays 返回指定数组的 “deep contents” 的字符串表示形式。
Arrays 检查两个数组是否相等。
Arrays 将此填充值分配给该数组的每个索引。
哈希码(原始数组) 返回此数组实例的整数 hashCode。
不匹配(数组1,数组2) 查找并返回两个指定数组之间第一个不匹配元素的索引。
parallelPrefix(originalArray, fromIndex, endIndex, functionalOperator) 使用指定的函数运算符对给定范围的数组执行parallelPrefix。
parallelPrefix(originalArray, operator) 使用指定的函数运算符对完整数组执行parallelPrefix。
parallelSetAll(originalArray, functionalGenerator) 使用提供的生成器函数并行设置该数组的所有元素。
Arrays 使用并行排序对指定数组进行排序。
setAll(originalArray, functionalGenerator) 使用提供的生成器函数设置指定数组的所有元素。
Arrays 按升序对完整数组进行排序。
Arrays 按升序对指定范围的数组进行排序。
Arrays 根据指定比较器产生的顺序对指定对象数组的指定范围进行排序。
Arrays 根据指定比较器产生的顺序对指定的对象数组进行排序。
spliterator(originalArray) 返回覆盖所有指定数组的 Spliterator。
spliterator(originalArray, fromIndex, endIndex) 返回覆盖指定数组的指定范围的数组类型的 Spliterator。
Arrays 返回以指定数组作为源的顺序流。
Arrays 它返回此数组内容的字符串表示形式。字符串表示形式由数组元素列表组成,括在方括号 (“[]”) 中。相邻元素由逗号后跟空格的字符分隔。元素通过 String.valueOf() 函数转换为字符串。

执行:

示例 1: Arrays 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)); 
    } 
}
输出
Integer Array as List: [[I@2f4d3709]

示例 2:Arrays.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: 二进制搜索(数组,fromIndex,toIndex,键,比较器)方法

此方法使用二分搜索算法在指定数组的范围内搜索指定对象。

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:比较(数组1,数组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(数组 1, 数组 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: Arrays copyOf()方法

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: Java.util.Arrays.copyOfRange()方法

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:Java.util.Arrays.deepEquals()方法

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: Arrays.deepToString()方法

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: Java.util.Arrays.equals()方法

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: Arrays.fill()方法

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:不匹配(数组1,数组2)方法

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:Arrays parallelSort()方法

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: Arrays.sort()方法

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: Arrays.sort()方法

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: Arrays.sort()方法

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<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: Arrays.sort()方法

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


// 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: Arrays stream()方法

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:Arrays.toString()方法

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]


相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Arrays class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。