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]
相關用法
- Java Arrays asList()用法及代碼示例
- Java Arrays copyOf()用法及代碼示例
- Java Arrays.deepToString()用法及代碼示例
- Java Arrays.fill()用法及代碼示例
- Java Arrays.sort()用法及代碼示例
- Java Arrays stream()用法及代碼示例
- Java Arrays.toString()用法及代碼示例
- Java Arrays.binarySearch()方法用法及代碼示例
- Java Arrays.toString()和Arrays.deepToString()的區別用法及代碼示例
- Java Arrays compare()用法及代碼示例
- Java ArrayList add()用法及代碼示例
- Java ArrayList addAll()用法及代碼示例
- Java ArrayList clear()用法及代碼示例
- Java ArrayList clone()用法及代碼示例
- Java ArrayList contains()用法及代碼示例
- Java ArrayList get()用法及代碼示例
- Java ArrayList indexOf()用法及代碼示例
- Java ArrayList removeAll()用法及代碼示例
- Java ArrayList remove()用法及代碼示例
- Java ArrayList size()用法及代碼示例
- Java ArrayList isEmpty()用法及代碼示例
- Java ArrayList subList()用法及代碼示例
- Java ArrayList set()用法及代碼示例
- Java ArrayList sort()用法及代碼示例
- Java ArrayList toArray()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Arrays class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。