17_面向对象api之Arrays类[操作系统入门]
arrays类概述">1. Arrays类概述
前面我们已经学习过数组, 数组是用来存放多个同类型元素的容器. 在完成数组的初始化后, 我们需要对数组中的元素做各种各样的操作, 比如数组元素排序, 遍历, 搜索指定元素等. 这些操作都是很基础的, 所以jdk为我们将这些操作封装为一个类, 即Arrays类, 数组操作的功具类.
Arrays类位于java.util包下, 在使用的时候需要导包.
2. Arrays成员分析
2.1 成员变量
Arrays设计为一个工具类, 主要用来对数组进行操作, 没有设计成员变量.
2.2 构造器
// Suppresses default constructor, ensuring non-instantiability. private Arrays() {}
构造器私有, 不能实例化.
2.3 成员方法
由于对数组的操作, 源码中涉及数据结构和算法的知识. 本人知识有限, 在系统的学习了数据结构和算法之后再深入研究其源码, 本次只作api的整理, 面向对象嘛, 先会用再说, 具体深层的知识需要进一步学习.
2.3.1 public static void sort(int[] a) 数组排序
/** * Sorts the specified array into ascending numerical order.//升序排列
*
* @implNote The sorting algorithm is a Dual-Pivot Quicksort
* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
* offers O(n log(n)) performance on all data sets, and is typically
* faster than traditional (one-pivot) Quicksort implementations.
*
* @param a the array to be sorted
*/
public static void sort(int[] a) {
DualPivotQuicksort.sort(a, 0, 0, a.length); //快排
}
本方法实现了对数组元素进行升序排列, 底层算法为快速排序, 该方法有大量重载,根据需求查询api使 用对应的方法. Demo如下
//数组元素排序 @Test
public void test1() {
int[] arr = {99, 88, 77, 66, 55, 44, 33, 22, 11};
Arrays.sort(arr); //排序
for(int i : arr) {
System.out.print(i + " "); //11 22 33 44 55 66 77 88 99
}
}
2.3.2 public static int binarySearch(long[] a, long key) 查找元素
/** * Searches the specified array of longs for the specified value using the
* binary search algorithm. The array must be sorted (as
* by the {@link #sort(long[])} method) prior to making this call. If it
* is not sorted, the results are undefined. If the array contains
* multiple elements with the specified value, there is no guarantee which
* one will be found.
*
* @param a the array to be searched
* @param key the value to be searched for
* @return index of the search key, if it is contained in the array;
*/
public static int binarySearch(long[] a, long key) {
return binarySearch0(a, 0, a.length, key);
}
功能: 二分查找指定的元素, 如果找到返回索引, 如果不包含此元素返回值为-1, 此方法有大量重载, 根据实际需求选用
@Test public void test2() {
int[] arr = {99, 88, 77, 66, 55, 44, 33, 22, 11};
int index = Arrays.binarySearch(arr, 55);
System.out.println(index); //4
}
2.3.3 public static boolean equals(int[] a, int[] a2) 判断两数组相等
两数组相等, 指元素个数相同, 且对应位置元素都相等.
- 源码
/** * Returns {@code true} if the two specified arrays of ints are
* <i>equal</i> to one another. Two arrays are considered equal if both
* arrays contain the same number of elements, and all corresponding pairs
* of elements in the two arrays are equal. In other words, two arrays
* are equal if they contain the same elements in the same order. Also,
* two array references are considered equal if both are {@code null}.
*
* @param a one array to be tested for equality
* @param a2 the other array to be tested for equality
* @return {@code true} if the two arrays are equal
*/
public static boolean equals(int[] a, int[] a2) {}
- Demo
@Test public void test3() {
int[] arr = {99, 88, 77, 66, 55, 44, 33, 22, 11};
int[] arr2 = {99, 88, 77, 66, 55, 44, 33, 22, 11};
int[] arr3 = {99, 88, 77, 66, 55, 44, 33,};
System.out.println(Arrays.equals(arr, arr2)); //true
System.out.println(Arrays.equals(arr, arr3)); //false
}
2.3.4 void fill(int[] a, int val) 填充元素值
- 源码
/** * Assigns the specified int value to each element of the specified array
* of ints.
*
* @param a the array to be filled
* @param val the value to be stored in all elements of the array
*/
public static void fill(int[] a, int val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}
- Demo
@Test public void test4() {
int[] arr = new int[10];
Arrays.fill(arr, 2); //填充数据
for(int i : arr) {
System.out.print(i + " "); //2 2 2 2 2 2 2 2 2 2
}
}
2.3.5 public static String toString(int[] a)
- 源码
/** * Returns a string representation of the contents of the specified array.
* The string representation consists of a list of the array‘s elements,
* enclosed in square brackets ({@code "[]"}). Adjacent elements are
* separated by the characters {@code ", "} (a comma followed by a
* space). Elements are converted to strings as by
* {@code String.valueOf(int)}. Returns {@code "null"} if {@code a} is
* {@code null}.
*
* @param a the array whose string representation to return
* @return a string representation of {@code a}
* @since 1.5
*/
public static String toString(int[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append(‘[‘);
for (int i = 0; ; i++) {
b.append(a[i]);
if (i == iMax)
return b.append(‘]‘).toString();
b.append(", ");
}
}
- demo
@Test public void test5() {
int[] arr = {99, 88, 77, 66, 55, 44, 33, 22, 11};
String str = Arrays.toString(arr);
System.out.println(str); //[99, 88, 77, 66, 55, 44, 33, 22, 11]
}
2.3.6 public static int compare(int[] a, int[] b)
- 源码: 比较两个数组, 如果长度相等逐个比较数值, 长度不等,比较长度
public static int compare(int[] a, int[] b) { if (a == b)
return 0;
if (a == null || b == null)
return a == null ? -1 : 1;
int i = ArraysSupport.mismatch(a, b,
Math.min(a.length, b.length));
if (i >= 0) {
return Integer.compare(a[i], b[i]);
}
return a.length - b.length;
}
- Demo
@Test public void test6() {
int[] arr2 = {99, 88, 77, 66, 55, 44, 33, 22, 11};
int[] arr3 = {99, 88, 77, 66, 55, 44, 33,};
int compare = Arrays.compare(arr2, arr3);
System.out.println(compare); //2
}
3. 总结
Arrays作为一个工具类, 为我们提供了丰富的操作数组的方法, 在实际开发中有大量应用. 有空可以多看看一下api, 熟悉一下方法.
17_面向对象api之Arrays类
原文:https://www.cnblogs.com/coder-Joe/p/13676468.html
以上是 17_面向对象api之Arrays类[操作系统入门] 的全部内容, 来源链接: utcz.com/z/519341.html