几个常用方法
boolean equals(int[] a,int[] b)
:判断两个数组是否相等(比较内容)String toString(int[] a)
:输出数组信息void fill(int[] a,int val)
:将指定值填充到数组之中void sort(int[] a)
:对数组进行排序int binarySearch(int[] a,int key)
:对排序后的数组进行二分检索指定的值,返回索引位置,找不到的话返回值比较麻烦,要判断[1] 搜索值不是数组元素,且在数组范围内,从1开始计数,得“ - 插入点索引值”;
[2] 搜索值不是数组元素,且大于数组内元素,索引值为 – (length + 1);
[3] 搜索值不是数组元素,且小于数组内元素,索引值为 – 1。1
2
3
4
5
6int a[] = new int[] {1, 3, 4, 6, 8, 9};
int x1 = Arrays.binarySearch(a, 5);
int x2 = Arrays.binarySearch(a, 4);
int x3 = Arrays.binarySearch(a, 0);
int x4 = Arrays.binarySearch(a, 10);
//结果为:x1=-4 x2=2 x3=-1 x4=-7
根据数组创建ArrayList
1
2
3
4String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
// [a, b, c, d, e]
System.out.println(arrayList);检查数组是否包含某个值
1
2
3
4String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
// true
System.out.println(b);将ArrayList转换为数组
1
2
3
4
5
6String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
for (String s : stringArr)
System.out.println(s);将数组转换为Set
1
2
3Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
//[d, e, b, c, a]
System.out.println(set);