看到网上的一段关于对数组操作的代码,觉得有用,在此备用。
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
public class MyArrayUtils {
public static int[] swap(int[] ints, int x, int y) {
int temp = ints[x];
ints[x] = ints[y];
ints[y] = temp;
return ints;
}
public static int[] bubbleSort(int[] source) {
for (int i = source.length - 1; i>0 ; i--) {
for (int j = 0; j < i; j++) {
if (source[j] > source[j + 1]) {
swap(source, j, j + 1);
}
}
}
return source;
}
public static int[] selectSort(int[] source) {
for (int i = 0; i < source.length; i++) {
for (int j = i + 1; j < source.length; j++) {
if (source[i] > source[j]) {
swap(source, i, j);
}
}
}
return source;
}
public static int[] insertSort(int[] source) {
for (int i = 1; i < source.length; i++) {
for (int j = i; (j > 0) && (source[j] < source[j - 1]); j--) {
swap(source, j, j - 1);
}
}
return source;
}
public static int[] quickSort(int[] source) {
return qsort(source, 0, source.length - 1);
}
private static int[] qsort(int source[], int low, int high) {
int i, j, x;
if (low < high) {
i = low;
j = high;
x = source[i];
while (i < j) {
while (i < j && source[j] > x) {
j--;
}
if (i < j) {
source[i] = source[j];
i++;
}
while (i < j && source[i] < x) {
i++;
}
if (i < j) {
source[j] = source[i];
j--;
}
}
source[i] = x;
qsort(source, low, i - 1);
qsort(source, i + 1, high);
}
return source;
}
// /////////////////////////////////////////////
// 排序算法结束
// ////////////////////////////////////////////
public static int[] binarySearch(int[] source) {
int i,j;
int low, high, mid;
int temp;
for (i = 0; i < source.length; i++) {
temp=source[i];
low=0;
high=i-1;
while (low <= high) {
mid = (low + high)/2;
if (source[mid]>temp) {
high=mid-1;
} else {
low = mid + 1;
}
}
for (j= i-1; j>high;j--)
source[j+1]=source[j];
source[high+1]=temp;
}
return source;
}
public static int[] reverse(int[] source) {
int length = source.length;
int temp = 0;
for (int i = 0; i < length >> 1; i++) {
temp = source[i];
source[i] = source[length - 1 - i];
source[length - 1 - i] = temp;
}
return source;
}
public static int[] insert(int[] array, int index, int insertNumber) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException();
}
if (index - 1 > array.length || index <= 0) {
throw new IllegalArgumentException();
}
int[] dest = new int[array.length + 1];
System.arraycopy(array, 0, dest, 0, index - 1);
dest[index - 1] = insertNumber;
System.arraycopy(array, index - 1, dest, index, dest.length - index);
return dest;
}
public static int[] remove(int[] array, int index) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException();
}
if (index > array.length || index <= 0) {
throw new IllegalArgumentException();
}
int[] dest = new int[array.length - 1];
System.arraycopy(array, 0, dest, 0, index - 1);
System.arraycopy(array, index, dest, index - 1, array.length - index);
return dest;
}
public static int[] merge(int[] array1, int[] array2) {
int[] dest = new int[array1.length + array2.length];
System.arraycopy(array1, 0, dest, 0, array1.length);
System.arraycopy(array2, 0, dest, array1.length, array2.length);
return dest;
}
public static int[] offsetArray(int[] array, int offset) {
int length = array.length;
int moveLength = length - offset;
int[] temp = Arrays.copyOfRange(array, moveLength, length);
System.arraycopy(array, 0, array, offset, moveLength);
System.arraycopy(temp, 0, array, 0, offset);
return array;
}
public static List shuffle(List list) {
java.util.Collections.shuffle(list);
return list;
}
public int[] shuffle(int[] array) {
Random random = new Random();
for (int index = array.length - 1; index >= 0; index--) {
// 从0到index处之间随机取一个值,跟index处的元素交换
exchange(array, random.nextInt(index + 1), index);
}
return array;
}
// 交换位置
private void exchange(int[] array, int p1, int p2) {
int temp = array[p1];
array[p1] = array[p2];
array[p2] = temp;
}
private static List mergeByList(int[] a, int[] b) {
// 用于返回的新数组,长度可能不为a,b数组之和,因为可能有重复的数字需要去掉
List c = new ArrayList();
// a数组下标
int aIndex = 0;
// b数组下标
int bIndex = 0;
// 对a、b两数组的值进行比较,并将小的值加到c,并将该数组下标+1,
// 如果相等,则将其任意一个加到c,两数组下标均+1
// 如果下标超出该数组长度,则退出循环
while (true) {
if (aIndex > a.length - 1 || bIndex > b.length - 1) {
break;
}
if (a[aIndex] < b[bIndex]) {
c.add(a[aIndex]);
aIndex++;
} else if (a[aIndex] > b[bIndex]) {
c.add(b[bIndex]);
bIndex++;
} else {
c.add(a[aIndex]);
aIndex++;
bIndex++;
}
}
// 将没有超出数组下标的数组其余全部加到数组c中
// 如果a数组还有数字没有处理
if (aIndex <= a.length - 1) {
for (int i = aIndex; i <= a.length - 1; i++) {
c.add(a[i]);
}
// 如果b数组中还有数字没有处理
} else if (bIndex <= b.length - 1) {
for (int i = bIndex; i <= b.length - 1; i++) {
c.add(b[i]);
}
}
return c;
}
private static int[] mergeByArray(int[] a, int[] b) {
int[] c = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length) {
if (a[i] <= b[j]) {
if (a[i] == b[j]) {
j++;
} else {
c[k] = a[i];
i++;
k++;
}
} else {
c[k] = b[j];
j++;
k++;
}
}
while (i < a.length) {
c[k] = a[i];
k++;
i++;
}
while (j < b.length) {
c[k] = b[j];
j++;
k++;
}
return c;
}
private static Map mergeByTreeMap(int[] a, int[] b) {
Map map = new TreeMap();
for (int i = 0; i < a.length; i++) {
map.put(a[i], a[i]);
}
for (int i = 0; i < b.length; i++) {
map.put(b[i], b[i]);
}
return map;
}
public static String print(int[] array) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; i++) {
sb.append("," + array[i]);
}
System.out.println("n"+sb.toString().substring(1));
return sb.toString().substring(1);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



