写在前面:这里涉及int数组的相关操作,没有操作其他数组,甚至是对象数组均未涉及。如未能帮助到你,抱歉哈。另外,大佬们请略过。我觉得很适合学习Java基础的小伙伴参考,你可以自己实现一下相关功能,甚至可以拓展,就当作是练习编程题了。个人能力有限,有错误敬请小伙伴指正。(因为我也不是大佬,哈哈)
1.ArrayUtils包含的功能:(arr表示被操作的数组)
1.isNull() 判断arr是否null
2.isEmpty() 判断arr是否是空数组,(数组的引用不能是null)
3.indexOf() 返回元素在arr中第一次出现的下标,不存在则返回-1
4.lastIndexOf()返回元素在arr中最后一次出现的下标,不存在则返回-1
5.contains() 判断arr中是否包含某元素,是则返回true,否则false
6.getMaxValue()返回arr中最大值
7.getMinValue()返回arr中最小值
8.swap() 交换arr中i,j下标的元素
9.arrayCopy() 传入两个数组src和dest,将src中srcPos下标开始往后数length个元素复制到dest,从destPos位置开始。(这个其实是System.arraycopy(),注意这个特殊,不是驼峰命名法。copy的c不要大写)
10.binarySearch() 对有序数组进行二分查找,返回待查找元素的下标
11.add() 在arr末尾添加一个值,返回一个新数组
12.addAll() 在arr末尾添加一个数组所有元素,返回一个新数组
13.insert() 在arr某位置插入一个值,返回一个新数组
14.insertAll() 在arr某位置插入一个数组所有的值,返回一个新数组
15.reverse() 将数组反转。我这里传入一个boolean区分:true则反转后原数组的值改变;false则反转后原数组不变,返回新数组
16.remove() 移除arr某位置的一个元素,返回新数组
17.removeAll() 移除arr某位置开始到末尾所有元素,返回新数组
18.removeElement() 移除arr第一次出现element的元素,返回新数组
19.removeElements() 移除arr中所有的值是element的元素,返回新数组
20.subarray() 截取arr的子数组,返回新数组
21.arrayToString()返回数组的字符串值。[value1,value2,...value,...]
2.
具体实现 :毕竟是Java基础,代码很容易理解,就不一点一点写解析了。可以看看代码注释哟。
使用:采用单例模式创建对象,创建工具类对象用
ArrayUtils arrayUtils = ArrayUtils.getInstance();
下面是代码实现:
/**
* @Author: gtbms
* @Date: 2023-02-08 21:57:29
* @Last Modified by: gtbms
* @Last Modified time: 2023-02-09 21:19:28
*/
//ArrayUtils contains common operations on arrays
public class ArrayUtils {
private static ArrayUtils arrayUtils;
private ArrayUtils() {
}
public static ArrayUtils getInstance() {
if (arrayUtils == null) {
arrayUtils = new ArrayUtils();
}
return arrayUtils;
}
public void arrayCopy(int[] srcArr, int srcPos, int[] destArr, int destPos, int length) {
System.arraycopy(srcArr, srcPos, destArr, destPos, length);
}
/**
* 1. Add
* The add() series of methods adds elements to an array,
* which can be a single number or an array of the same type
* 1. Added at the end by default
* 2. You can specify the insertion position
* 3. Special circumstances:
* Cannot operate if array == null
* Added values should be of the same type,
* e.g. int cannot add float values
*/
public int[] add(int[] arr, int e) {
int arrLen = arr.length;
int[] newArr = new int[arrLen + 1];
arrayCopy(arr, 0, newArr, 0, arrLen);
newArr[arrLen] = e;
return newArr;
}
public int[] addAll(int[] srcArr1, int[] srcArr2) {
int srcLen1 = srcArr1.length;
int srcLen2 = srcArr2.length;
int mergeLength = srcLen1 + srcLen2;
int[] mergeArr = new int[mergeLength];
// Copy the original array first
arrayCopy(srcArr1, 0, mergeArr, 0, srcLen1);
// Add the target array
arrayCopy(srcArr2, 0, mergeArr, srcLen1, srcLen2);
return mergeArr;
}
/**
* 2. Search for a value
* 2.1 Binary search for the subscript of element e
* Note that this lookup is for an ordered array
*/
public int binarySearch(int[] arr, int e) {
int left = 0, right = arr.length - 1;
int mid = -1;
while (left <= right) {
mid = (left + right) / 2;
if (arr[mid] < e) {
left = mid + 1;
} else if (arr[mid] == e) {
return mid;
} else {
right = mid - 1;
}
}
return -mid - 1; // - (mid -1) -1 indicates the insertion position, that is mid
}
/**
* 2.2 Query whether a specified value exists in an array
* starting from the first digit of the array.
* There is a value that returns index, otherwise -1 is returned.
*/
public int indexOf(int[] arr, int tar) {
int arrLen = arr.length;
for (int i = 0; i < arrLen; i++) {
if (arr[i] == tar) {
return i;
}
}
return -1;
}
private boolean isNull(int[] arr) {
return arr == null;
}
/**
* 2.3Checks whether the data exists in the array
* and returns a boolean value.
*/
public boolean contains(int[] arr, int e) {
return indexOf(arr, e) != -1;
}
/**
* 2.4 Finding the last occurrence position of an element in an array
*/
public int lastIndexOf(int[] arr, int tar) {
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] == tar) {
return i;
}
}
return -1;
}
/**
* 2.5 Array null
* The null condition here is arr.length == 0 or arr == null
*/
public boolean isEmpty(int[] arr) {
return arr == null || arr.length == 0;
}
/**
* 2.6 Obtaining the maximum value of an Array.
*/
public int getMaxValue(int[] arr) {
//Integer.MIN_VALUE is returned if the array is empty
if (isEmpty(arr)) {
return Integer.MIN_VALUE;
}
int arrLen = arr.length;
int max = arr[0];
for (int i = 1; i < arrLen; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}
/**
* 2.7 Obtaining the minimum Array Value
*/
public int getMinValue(int[] arr) {
//Integer.MIN_VALUE is returned if the array is empty
if (isEmpty(arr)) {
return Integer.MAX_VALUE;
}
int arrLen = arr.length;
int min = arr[0];
for (int i = 1; i < arrLen; i++) {
if (min > arr[i]) {
min = arr[i];
}
}
return min;
}
/**
* 3. The following methods operate on arrays
* 3.1 Exchange of array elements
*/
public void swap(int[] arr, int pos1, int pos2) {
if (isEmpty(arr)) {
return;
}
int temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}
/**
* 3.2 Reversing an Array
* Implement array element inversion
* If original is true, the original array is reversed,
* The original array value is swapped back and forth.
* If original is false, the original array is unchanged,
* And returns the new array after inversion
*/
public int[] reverse(int[] arr, boolean original) {
int[] dest;
if (original) {
reverse1(arr);
dest = arr;
} else {
dest = reverse2(arr);
}
return dest;
}
/**
* reverse1
* Invert the array to operate on the original array
*/
private void reverse1(int[] arr) {
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
swap(arr, i, j);
}
}
/**
* reverse2
* After reversing the array, the original array is unchanged,
* Assigns the inverted result to the new array
*/
private int[] reverse2(int[] arr) {
int arrLen = arr.length;
int[] newArr = new int[arrLen];
for (int i = 0; i < arrLen; i++) {
newArr[i] = arr[arrLen - 1 - i];
}
return newArr;
}
/**
* 3.3 Insert a value into the pos position in the array and return a new array.
*/
public int[] insert(int[] arr, int pos, int e) {
int arrLen = arr.length;
int[] newArr;
if (pos >= 0 && pos < arrLen) {
newArr = new int[arrLen + 1];
arrayCopy(arr, 0, newArr, 0, pos);
newArr[pos] = e;
arrayCopy(arr, pos, newArr, pos + 1, arrLen - pos);
} else {
newArr = new int[arrLen];
arrayCopy(arr, 0, newArr, 0, arrLen);
}
return newArr;
}
/**
* 3.4 Insert an array at the position specified by arr
*/
public int[] insertAll(int[] arr, int pos, int[] e) {
int arrLen = arr.length;
int eLen = e.length;
int[] newArr;
if (pos >= 0 && pos < arrLen) {
newArr = new int[arrLen + eLen];
arrayCopy(arr, 0, newArr, 0, pos);
arrayCopy(e, 0, newArr, pos, eLen);
arrayCopy(arr, pos, newArr, pos + eLen, arrLen - pos);
return newArr;
} else {
newArr = new int[arrLen];
arrayCopy(arr, 0, newArr, 0, arrLen);
}
return newArr;
}
/**
* 3.5 Deleting elements in the specified position of the arr array
* returns a new array
*/
public int[] remove(int[] arr, int pos) {
int arrLen = arr.length;
int[] afterDelete;
if (pos >= 0 && pos < arr.length) {
afterDelete = new int[arrLen - 1];
arrayCopy(arr, 0, afterDelete, 0, pos);
arrayCopy(arr, pos + 1, afterDelete, pos, arrLen - 1 - pos);
return afterDelete;
} else {
afterDelete = new int[arrLen];
arrayCopy(arr, 0, afterDelete, 0, arrLen);
}
return afterDelete;
}
/**
* 3.6 Remove all elements after the specified position pos
* and return a new array
*/
public int[] removeAll(int[] arr, int pos) {
int arrLen = arr.length;
int[] afterDelete;
if (pos >= 0 && pos < arr.length) {
afterDelete = new int[pos];
arrayCopy(arr, 0, afterDelete, 0, pos);
} else {
afterDelete = new int[arrLen];
arrayCopy(arr, 0, afterDelete, 0, arrLen);
}
return afterDelete;
}
/**
* 3.7 Remove the element whose value appears for the first time in arr
* and return a new array
*/
public int[] removeElement(int[] arr, int element) {
int arrLen = arr.length;
int index = indexOf(arr, element);
int[] newArr;
// Without this element, the original array arr remains unchanged after removal
if (index == -1) {
newArr = new int[arrLen];
arrayCopy(arr, 0, newArr, 0, arrLen);
} else {
newArr = remove(arr, index);
}
return newArr;
}
/**
* 3.8 Removing all elements of the array arr is value then returns a new array
*/
public int[] removeElements(int[] arr, int element) {
int[] temp = arr;
int tempLen;
int index;
int[] newArr;
while (true) {
tempLen = temp.length;
index = indexOf(temp, element);
//index == -1 indicates that temp does not have this element
if (index == -1) {
newArr = new int[tempLen];
arrayCopy(temp, 0, newArr, 0, tempLen);
return newArr;
}
//index! If the value is -1, a value is removed, and the value is reassigned to temp
else {
temp = remove(temp, index);
}
}
}
/**
* 3.9 Intercepting a subarray (range: [begin,end))
*/
public int[] subarray(int[] arr, int begin, int end) {
int arrLen = arr.length;
int[] newArr;
if (begin > end) {
newArr = null;
} else if ((begin < 0 || begin > arrLen) || end > arrLen) {
newArr = null;
} else {
newArr = new int[end - begin];
arrayCopy(arr, begin, newArr, 0, end - begin);
}
return newArr;
}
/**
* 3.10 Truncate subarray from begin to end of array
*/
public int[] subarray(int[] arr, int begin) {
int arrLen = arr.length;
int[] newArr;
if (begin < 0 || begin >= arrLen) {
newArr = null;
} else {
newArr = subarray(arr, 0, arrLen);
}
return newArr;
}
/**
* 4. Implement array traversal
*/
public String arrayToString(int[] arr) {
if (isNull(arr)) {
return "null";
}
StringBuilder sb = new StringBuilder();
sb.append('[');
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]);
if (i < arr.length - 1) {
sb.append(',');
}
}
sb.append(']');
return sb.toString();
}
}
好啦,一起进步吧。有错误的或者觉得哪里不合理的可以聊一聊哈!