标签:compare 递归调用 字符串 ati 大于 z-index false ctc name
public class Arrays extends Object
public class Arrays extends Object
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);//注意,这里的ArrayList不是我们通常用的ArrayList,而是Arrays类中定义的一个内部类
}
private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable {
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);//如果 array 为 null 抛出 NullPointerException,否则直接将 array 的引用赋给 a
//因为 a 和 array 这两个数组引用的是同一块内存地址,且后续对此 ArrayList 的操作实际都是对 a 的操作,所以也会影响到原始数组
}
}
//因为此 ArrayList 没有重新 AbstractList 中的 add、remove、clear等方法,所以不能调用这几个方法,否则都是直接抛出异常
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
}
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);//注意,这里的ArrayList不是我们通常用的ArrayList,而是Arrays类中定义的一个内部类
}
private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable {
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);//如果 array 为 null 抛出 NullPointerException,否则直接将 array 的引用赋给 a
//因为 a 和 array 这两个数组引用的是同一块内存地址,且后续对此 ArrayList 的操作实际都是对 a 的操作,所以也会影响到原始数组
}
}
//因为此 ArrayList 没有重新 AbstractList 中的 add、remove、clear等方法,所以不能调用这几个方法,否则都是直接抛出异常
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
}
String[] array = new String[] { "a", "b", };
List<String> mList = Arrays.asList(array);
String[] array = new String[] { "a", "b", };
List<String> mList = Arrays.asList(array);
//1、不可以将 List 强转为 ArrayList .从上述源码分析可知,这两个 ArrayList 并不是同一个类,所以不能强转
ArrayList<String> list2 = (ArrayList<String>) list;//ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
//2、对返回列表的更改会“直接写”到数组,所以当修改转换后的集合中的元素时,原始数组中的元素也会更改
mList.set(0, "包青天");
System.out.println(Arrays.toString(array));//[包青天, b]
//3、因为数组的长度不能改变,所以对转换而来的集合的长度改变的操作都是不合法的,会报 java.lang.UnsupportedOperationException
mList.remove(0);//此 ArrayList 没有
mList.add("111");
list.clear();
//1、不可以将 List 强转为 ArrayList .从上述源码分析可知,这两个 ArrayList 并不是同一个类,所以不能强转
ArrayList<String> list2 = (ArrayList<String>) list;//ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
//2、对返回列表的更改会“直接写”到数组,所以当修改转换后的集合中的元素时,原始数组中的元素也会更改
mList.set(0, "包青天");
System.out.println(Arrays.toString(array));//[包青天, b]
//3、因为数组的长度不能改变,所以对转换而来的集合的长度改变的操作都是不合法的,会报 java.lang.UnsupportedOperationException
mList.remove(0);//此 ArrayList 没有
mList.add("111");
list.clear();
String[] array = new String[] { "c", "a", "b", "ab", "aa", "a" };
Arrays.sort(array);//集合中不能有 null ,否则会报NullPointerException,这种情况下需要自定义比较器
System.out.println(Arrays.toString(array));//[a, a, aa, ab, b, c]
array = new String[] { "c", "a", "b", "ab", "aa", "a" };
Arrays.sort(array, 0, 3);//只对 0-2 这三个元素排序
System.out.println(Arrays.toString(array));//[a, b, c, ab, aa, a]
String[] array = new String[] { "c", "a", "b", "ab", "aa", "a" };
Arrays.sort(array);//集合中不能有 null ,否则会报NullPointerException,这种情况下需要自定义比较器
System.out.println(Arrays.toString(array));//[a, a, aa, ab, b, c]
array = new String[] { "c", "a", "b", "ab", "aa", "a" };
Arrays.sort(array, 0, 3);//只对 0-2 这三个元素排序
System.out.println(Arrays.toString(array));//[a, b, c, ab, aa, a]
String[] array = new String[] { "c", "a", "b", "ab", "aa", null, "a" };
Comparator<String> c = (o1, o2) -> {
if (o1 == null) {
return 1;//把 null 放在最后面
} else {
if (o2 == null) {
return -1;//把 null 放在最后面
} else {
if (o1.length() != o2.length()) {
return o1.length() - o2.length();
} else {
return o1.compareTo(o2);
}
}
}
};
Arrays.sort(array, c);
System.out.println(Arrays.toString(array));//[a, a, b, c, aa, ab]
String[] array = new String[] { "c", "a", "b", "ab", "aa", null, "a" };
Comparator<String> c = (o1, o2) -> {
if (o1 == null) {
return 1;//把 null 放在最后面
} else {
if (o2 == null) {
return -1;//把 null 放在最后面
} else {
if (o1.length() != o2.length()) {
return o1.length() - o2.length();
} else {
return o1.compareTo(o2);
}
}
}
};
Arrays.sort(array, c);
System.out.println(Arrays.toString(array));//[a, a, b, c, aa, ab]
public static int binarySearch(byte[] a, byte key) {
return binarySearch0(a, 0, a.length, key);
}
public static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key) {
rangeCheck(a.length, fromIndex, toIndex);//检查 fromIndex 和 toIndex 的值是否合法,不合法抛出相应的异常
return binarySearch0(a, fromIndex, toIndex, key);
}
private static int binarySearch0(byte[] a, int fromIndex, int toIndex, byte key) {
int low = fromIndex;//最小位置
int high = toIndex - 1;//最大位置
while (low <= high) {
int mid = (low + high) >>> 1;//中间位置
byte midVal = a[mid];//中间位置的值
if (midVal < key) {//若中间位置的值小于要查询的值,向右边继续找
low = mid + 1;//最小位置=中间位置+1,最大位置不变
} else if (midVal > key) {//若中间位置的值大于要查询的值,向左边继续找
high = mid - 1;//最大位置=中间位置-1,最小位置不变
} else {
return mid; //否则这就是要找到的值
}
}
return -(low + 1); //如果没找到返回的值
}
public static int binarySearch(byte[] a, byte key) {
return binarySearch0(a, 0, a.length, key);
}
public static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key) {
rangeCheck(a.length, fromIndex, toIndex);//检查 fromIndex 和 toIndex 的值是否合法,不合法抛出相应的异常
return binarySearch0(a, fromIndex, toIndex, key);
}
private static int binarySearch0(byte[] a, int fromIndex, int toIndex, byte key) {
int low = fromIndex;//最小位置
int high = toIndex - 1;//最大位置
while (low <= high) {
int mid = (low + high) >>> 1;//中间位置
byte midVal = a[mid];//中间位置的值
if (midVal < key) {//若中间位置的值小于要查询的值,向右边继续找
low = mid + 1;//最小位置=中间位置+1,最大位置不变
} else if (midVal > key) {//若中间位置的值大于要查询的值,向左边继续找
high = mid - 1;//最大位置=中间位置-1,最小位置不变
} else {
return mid; //否则这就是要找到的值
}
}
return -(low + 1); //如果没找到返回的值
}
String[] array = new String[] { "c", "a", "b", "ab", "aa", "a" };
Arrays.sort(array);
System.out.println(Arrays.toString(array));//[a, a, aa, ab, b, c]
System.out.println(Arrays.binarySearch(array, "b"));//4
System.out.println(Arrays.binarySearch(array, "a"));//0
System.out.println(Arrays.binarySearch(array, "ac"));//-5【-(4+1)】
String[] array = new String[] { "c", "a", "b", "ab", "aa", "a" };
Arrays.sort(array);
System.out.println(Arrays.toString(array));//[a, a, aa, ab, b, c]
System.out.println(Arrays.binarySearch(array, "b"));//4
System.out.println(Arrays.binarySearch(array, "a"));//0
System.out.println(Arrays.binarySearch(array, "ac"));//-5【-(4+1)】
public static long[] copyOf(long[] original, int newLength) {
long[] copy = new long[newLength];//先定义一个指定长度的数组
//再通过 System.arraycopy() 方法将原始数组中的内容复制到上面定义的数组,然后直接返回
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
return copy;
}
public static long[] copyOf(long[] original, int newLength) {
long[] copy = new long[newLength];//先定义一个指定长度的数组
//再通过 System.arraycopy() 方法将原始数组中的内容复制到上面定义的数组,然后直接返回
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
return copy;
}
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] //直接构造 Object数组
: (T[]) Array.newInstance(newType.getComponentType(), newLength); //通过反射方式构造指定类型的数组
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); //和上面一样
return copy;
}
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] //直接构造 Object数组
: (T[]) Array.newInstance(newType.getComponentType(), newLength); //通过反射方式构造指定类型的数组
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); //和上面一样
return copy;
}
String[] array = new String[] { "c", "a", };
String[] array2 = Arrays.copyOf(array, 1);
String[] array3 = Arrays.copyOf(array, 3);
String[] array4 = Arrays.copyOfRange(array, 0, 1);
System.out.println(Arrays.toString(array2) + " " + Arrays.toString(array3) + " " + Arrays.toString(array4));//[c] [c, a, null] [c]
String[] array = new String[] { "c", "a", };
String[] array2 = Arrays.copyOf(array, 1);
String[] array3 = Arrays.copyOf(array, 3);
String[] array4 = Arrays.copyOfRange(array, 0, 1);
System.out.println(Arrays.toString(array2) + " " + Arrays.toString(array3) + " " + Arrays.toString(array4));//[c] [c, a, null] [c]
Object[] array = new Object[] { 1, 2 };
Integer[] array2 = Arrays.copyOf(array, 3, Integer[].class); //不能用于 int[] 等基本类型的数组,因为泛型不支持基本类型
System.out.println(Arrays.toString(array2));//[1, 2, null]
Object[] array = new Object[] { 1, 2 };
Integer[] array2 = Arrays.copyOf(array, 3, Integer[].class); //不能用于 int[] 等基本类型的数组,因为泛型不支持基本类型
System.out.println(Arrays.toString(array2));//[1, 2, null]
String[] b = { "b" };
String[] b2 = { "b" };
String[] qt = { "qt" };
String[][] name1 = { b, qt };
String[][] name2 = { b, qt };
System.out.println(Arrays.equals(name1, name2) + " " + Arrays.deepEquals(name1, name2));// true true
System.out.println(Arrays.equals(b2, b) + " " + Arrays.deepEquals(b2, b));// true true
String[][] name3 = { { "b" }, { "qt" } };
String[][] name4 = { { "b" }, { "qt" } };
System.out.println(Arrays.equals(name3, name4) + " " + Arrays.deepEquals(name3, name4));// false true
String[][] name5 = { b };
String[][] name6 = { b2 };
System.out.println(Arrays.equals(name5, name6) + " " + Arrays.deepEquals(name5, name6));// false true
String[] b = { "b" };
String[] b2 = { "b" };
String[] qt = { "qt" };
String[][] name1 = { b, qt };
String[][] name2 = { b, qt };
System.out.println(Arrays.equals(name1, name2) + " " + Arrays.deepEquals(name1, name2));// true true
System.out.println(Arrays.equals(b2, b) + " " + Arrays.deepEquals(b2, b));// true true
String[][] name3 = { { "b" }, { "qt" } };
String[][] name4 = { { "b" }, { "qt" } };
System.out.println(Arrays.equals(name3, name4) + " " + Arrays.deepEquals(name3, name4));// false true
String[][] name5 = { b };
String[][] name6 = { b2 };
System.out.println(Arrays.equals(name5, name6) + " " + Arrays.deepEquals(name5, name6));// false true
String[] b = { "b", "t", "t" };
Arrays.fill(b, "包");
System.out.println(Arrays.toString(b));//[包, 包, 包]
Arrays.fill(b, 1, 2, "青");// toIndex 不能用 1 ,因为 fromIndex==toIndex 时填充范围为空
Arrays.fill(b, 2, b.length, "天");//toIndex 不能大于 b.length,否则报 ArrayIndexOutOfBoundsException
System.out.println(Arrays.toString(b));//[包, 青, 天]
String[] b = { "b", "t", "t" };
Arrays.fill(b, "包");
System.out.println(Arrays.toString(b));//[包, 包, 包]
Arrays.fill(b, 1, 2, "青");// toIndex 不能用 1 ,因为 fromIndex==toIndex 时填充范围为空
Arrays.fill(b, 2, b.length, "天");//toIndex 不能大于 b.length,否则报 ArrayIndexOutOfBoundsException
System.out.println(Arrays.toString(b));//[包, 青, 天]
String[][] name = { { "b" }, { "qt" } };
System.out.println(Arrays.toString(name) + " " + Arrays.deepToString(name));
//[[Ljava.lang.String;@15db9742, [Ljava.lang.String;@6d06d69c] [[b], [qt]]
String[][] name = { { "b" }, { "qt" } };
System.out.println(Arrays.toString(name) + " " + Arrays.deepToString(name));
//[[Ljava.lang.String;@15db9742, [Ljava.lang.String;@6d06d69c] [[b], [qt]]
String[][] name = { { "b" }, { "qt" } };
System.out.println(Arrays.hashCode(name) + " " + Arrays.deepHashCode(name));//312355675 8610
String[][] name = { { "b" }, { "qt" } };
System.out.println(Arrays.hashCode(name) + " " + Arrays.deepHashCode(name));//312355675 8610
标签:compare 递归调用 字符串 ati 大于 z-index false ctc name
原文地址:https://www.cnblogs.com/baiqiantao/p/9269753.html