这个 阵列 上课 JAVAutil包 是这个世界的一部分 集合框架 .此类提供静态方法来动态创建和访问 Java数组 .它只包含静态方法和对象类的方法。这个类的方法可以由类名本身使用。
类层次结构如下所示:
java.lang.Object ↳ java.util.Arrays
极客,现在你们一定想知道,当我们能够声明、初始化和计算数组上的操作时,为什么我们需要java Arrays类。不过,这个问题的答案就在这个类的方法中,我们将进一步讨论这些方法,因为实际上,这些函数可以帮助程序员扩展数组的视野,例如,在 循环 用于在阵列上执行以下任务:
- 用特定值填充数组。
- 对数组进行排序。
- 在数组中搜索。
- 还有更多。
在这里,Arrays类提供了几个静态方法,可以用来直接执行这些任务,而无需使用循环,从而使我们的代码非常简短和优化。
语法: 类别声明
public class Arrays extends Object
语法: 为了使用数组
Arrays.<function name>;
Java数组类中的方法
的数组类 JAVAutil包 包含几个静态方法,可用于数组中的填充、排序、搜索等。现在让我们讨论一下这个类的方法,如下表所示:
方法 | 执行的动作 |
---|---|
asList() | 返回由指定数组支持的固定大小列表 |
二进制搜索() | 在二进制搜索算法的帮助下搜索数组中的指定元素 |
二进制搜索(数组、fromIndex、toIndex、键、比较器) | 使用二进制搜索算法在指定数组的范围内搜索指定对象 |
比较(数组1、数组2) | 按字典顺序比较作为参数传递的两个数组。 |
复印件(原件,新长度) | 复制指定的数组,使用默认值截断或填充(如有必要),使副本具有指定的长度。 |
copyOfRange(原始数组、fromIndex、endIndex) | 将指定数组的指定范围复制到新数组中。 |
深度相等(对象[]a1,对象[]a2) | 如果指定的两个数组彼此非常相等,则返回true。 |
deepHashCode(对象[]a) | 基于指定数组的“深层内容”返回哈希代码。 |
deepToString(对象[]a) | 返回指定数组的“深层内容”的字符串表示形式。 |
等于(数组1,数组2) | 检查两个数组是否相等。 |
填充(原始数组,填充值) | 将此填充值分配给此数组的每个索引。 |
哈希代码(原始数组) | 返回此数组实例的整数哈希代码。 |
不匹配(阵列1,阵列2) | 查找并返回两个指定数组之间第一个不匹配元素的索引。 |
parallelPrefix(原始数组、fromIndex、endIndex、functionalOperator) | 使用指定的函数运算符对数组的给定范围执行parallelPrefix。 |
parallelPrefix(原始数组,运算符) | 使用指定的函数运算符对完整数组执行parallelPrefix。 |
parallelSetAll(原始阵列、函数生成器) | 使用提供的生成器函数并行设置此数组的所有元素。 |
平行排序(原始数组) | 使用并行排序对指定数组进行排序。 |
setAll(原始数组,函数生成器) | 使用提供的生成器函数设置指定数组的所有元素。 |
排序(原始数组) | 按升序对整个数组排序。 |
排序(原始数组、fromIndex、endIndex) | 按升序对数组的指定范围进行排序。 |
排序(T[]a,int from index,int to index,Comparator |
根据指定比较器产生的顺序,对指定对象数组的指定范围进行排序。 |
排序(T[]a,比较器 |
根据指定比较器产生的顺序对指定的对象数组进行排序。 |
拆分器(原始阵列) | 返回覆盖所有指定数组的拆分器。 |
拆分器(原始数组、fromIndex、endIndex) | 返回数组类型的拆分器,该拆分器覆盖指定数组的指定范围。 |
溪流(原始阵列) | 返回以指定数组作为源的序列流。 |
toString(原始阵列) | 它返回此数组内容的字符串表示形式。字符串表示法由数组元素的列表组成,用方括号(“[])括起来。相邻元素之间用逗号和空格分隔。元素按字符串转换为字符串。valueOf()函数。 |
实施:
例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)); } } |
Integer Array as List: [[I@2f4d3709]
例2: 二进制搜索() 方法
这种方法借助二进制搜索算法搜索数组中的指定元素。
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: 比较(数组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: 复印件(原件,新长度) 方法
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( "New Arrays by copyOf:" ); 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(原始数组、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( "New Arrays by copyOfRange:" ); // 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: 深度相等(对象[]a1,对象[]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(对象[]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(对象[]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: 等于(数组1,数组2) 方法
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: 填充(原始数组,填充值) 方法
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: 不匹配(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: 平行排序(原始数组) 方法
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: 排序(原始数组) 方法
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: 排序(原始数组、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: 排序(T[]a,int from index,int to index,Comparator
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( "Sorted by rollno" ); for ( int i = 0 ; i < arr.length; i++) System.out.println(arr[i]); } } |
Unsorted111 bbbb london131 aaaa nyc121 cccc jaipurSorted by rollno111 bbbb london131 aaaa nyc121 cccc jaipur
例19: 排序(T[]a,比较器
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( "Sorted by rollno" ); for ( int i = 0 ; i < arr.length; i++) System.out.println(arr[i]); } } |
Unsorted111 bbbb london131 aaaa nyc121 cccc jaipurSorted by rollno111 bbbb london121 cccc jaipur131 aaaa nyc
例20: 拆分器(原始阵列)方法
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: 拆分器(原始数组、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: 溪流(原始阵列) 方法
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(原始阵列) 方法
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]
本文由 Rishabh Mahrsee .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以写一篇文章,然后将文章邮寄给评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论