该类包含用于操作数组的各种方法(例如排序和搜索)。此类还包含一个静态工厂,允许将数组视为列表。
如果指定的数组引用为null,则此类中的方法都抛出NullPointerException ,除非另有说明。
asList(T……a)方法
返回由指定数组支持的固定大小的列表。
此方法还提供了一种方便的方法来创建初始化为包含多个元素的固定大小的列表:
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
import java.util.Arrays;
import java.util.List;
public class Demo1 {
public static void main(String[] args) {
//创建初始化为包含多个元素的固定大小的列表
List<String> s = Arrays.asList("a","b","c","d","e","f","g");
System.out.println(s);
System.out.println("-----------------------");
System.out.println(Arrays.asList(3,5,7,12,43,6,79,5));
}
}
结果:
[a, b, c, d, e, f, g]
-----------------------
[3, 5, 7, 12, 43, 6, 79, 5]
Process finished with exit code 0
toString方法
返回指定数组内容的字符串表示形式。 字符串表示由数组元素的列表组成,用方括号括起来( "[]" )。 相邻元素由字符", " (逗号后跟空格)分隔。
参数:要返回其字符串表示形式的数组
import java.util.Arrays;
public class Demo2 {
public static void main(String[] args) {
int[] i = {5,4,3,6,8,11,2,10};
//未使用toString
System.out.println(i);
System.out.println("---------------");
//使用toString
System.out.println(Arrays.toString(i));
}
}
结果:
[I@7c30a502
---------------
[5, 4, 3, 6, 8, 11, 2, 10]
Process finished with exit code 0
sort方法
将指定的数组按升序排序。
参数:要排序的数组 。
import java.util.Arrays;
public class Demo3 {
public static void main(String[] args) {
int[] i = {5,4,3,6,8,11,2,10};
Arrays.sort(i);
System.out.println(Arrays.toString(i));
}
}
结果:
[2, 3, 4, 5, 6, 8, 10, 11]
Process finished with exit code 0
binarySearch方法
使用二进制搜索算法在指定的int数组中搜索指定的值。 在进行此调用之前,必须对数组进行排序(如sort(int[\])方法)。 如果未排序,则结果未定义。 如果数组包含具有指定值的多个元素,则无法保证找到哪个元素。
参数 :
a - 要搜索的数组
key - 要搜索的值
import java.util.Arrays;
public class Demo4 {
public static void main(String[] args) {
int[] i = {5,4,3,6,8,11,2,10};
Arrays.sort(i);
System.out.println(Arrays.toString(i));
System.out.println(Arrays.binarySearch(i,5));
}
}
结果:
[2, 3, 4, 5, 6, 8, 10, 11]
3
Process finished with exit code 0
compare方法
字典顺序比较两个阵列。
如果两个数组共享一个公共前缀,则字典比较是比较两个元素的结果,在相应数组中作为前缀长度的索引。 否则,一个数组是另一个数组的正确前缀,并且词典比较是比较两个数组长度的结果。
null阵列引用在字典上被视为小于非null阵列引用。 两个null数组引用被认为是相等的。
参数
a - 要比较的第一个数组
b - 要比较的第二个数组
结果
如果第一个和第二个数组相等并且包含相同顺序的相同元素,则值为0 ; 如果第一个数组按字典顺序小于第二个数组,则值小于0 ; 如果第一个数组按字典顺序大于第二个数组,则值大于0
import java.util.Arrays;
public class Demo5 {
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6,7};
int[] b = {1,2,3,4,5,6,7};
int[] c = {1,2,3,4,5,6};
int[] d = {1,2,3,4,5,6,7,8};
int[] e = {};
//a=b
System.out.println(Arrays.compare(a,b));
//a>c
System.out.println(Arrays.compare(a,c));
//c<d
System.out.println(Arrays.compare(c,d));
//d(非null)>e(null)
System.out.println(Arrays.compare(d,e));
}
}
结果:
0
1
-2
8
Process finished with exit code 0
copyOf方法
使用零复制指定的数组,截断或填充(如有必要),以使副本具有指定的长度。 对于在原始数组和副本中都有效的所有索引,这两个数组将包含相同的值。 对于在副本中有效但不在原件中有效的任何索引,副本将包含0 。 当且仅当指定的长度大于原始数组的长度时,这些索引才会存在。
参数
original - 要复制的数组
newLength - 要返回的副本的长度
结果
原始数组的副本,用零截断或填充以获得指定的长度
import java.util.Arrays;
public class Demo6 {
public static void main(String[] args) {
int[] a = {1,2,3,4,5};
System.out.println(a.length);
a = Arrays.copyOf(a,10);
System.out.println(a.length);
}
}
结果:
5
10
Process finished with exit code 0
本文介绍了Java中Arrays类的一些主要方法,包括asList()用于将数组转换为列表,toString()显示数组内容,sort()对数组进行排序,binarySearch()进行二分查找,compare()比较两个数组,以及copyOf()复制并调整数组长度。示例代码展示了这些方法的使用方法和输出结果。
2100

被折叠的 条评论
为什么被折叠?



