学而不思则罔,思而不学则殆
【Java】Arrays
toString 数组转化为字符串
时间复杂度 O ( n ) O(n) O(n)
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(", ");
}
}
int[] ints = new int[]{4, 3, 2, 1};
System.out.println(Arrays.toString(ints));//[4, 3, 2, 1]
Person[] people = new Person[]{
new Person("zy1", 4),
new Person("zy2", 3),
new Person("zy3", 2),
new Person("zy4", 1)
};
System.out.println(Arrays.toString(people));//[Person{name='zy1', age=1}, Person{name='zy2', age=1}, Person{name='zy3', age=1}, Person{name='zy4', age=1}]
fill 填充数组
时间复杂度 O ( n ) O(n) O(n)
public static void fill(int[] a, int val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}
private static void testFill() {
int[] ints = new int[4];
System.out.println(Arrays.toString(ints));//[0, 0, 0, 0]
Arrays.fill(ints, 2);
System.out.println(Arrays.toString(ints));//[2, 2, 2, 2]
double[] doubles = new double[4];
System.out.println(Arrays.toString(doubles));//[0.0, 0.0, 0.0, 0.0]
Arrays.fill(doubles, 100);
System.out.println(Arrays.toString(doubles));//[100.0, 100.0, 100.0, 100.0]
}
equals 是否相等
时间复杂度 O ( n ) O(n) O(n)
public static boolean equals(Object[] a, Object[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
for (int i=0; i<length; i++) {
Object o1 = a[i];
Object o2 = a2[i];
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return true;
}
int[] ints1 = new int[]{1, 2, 3, 4};
int[] ints2 = new int[]{1, 2, 3, 4};
System.out.println(Arrays.equals(ints1, ints2));//true
String[] strings1 = new String[]{"a", "ab", "ac", "af"};
String[] strings2 = new String[]{"a", "ab", "ac", "af"};
System.out.println(Arrays.equals(strings1, strings2));//true
String[] strings3 = strings1;
String[] strings4 = new String[]{"a", "ab", "ac", "af3"};
System.out.println(Arrays.equals(strings3, strings4));//false
asList 数组转化为list
List<Integer> integers = Arrays.asList(1, 2, 3, 4);
System.out.println(integers.getClass() + " " + integers);//class java.util.Arrays$ArrayList [1, 2, 3, 4]
List<Person> people = Arrays.asList(new Person("zhangsan", 1), new Person("lisi", 2));
System.out.println(people.getClass() + " " + people);//class java.util.Arrays$ArrayList [Person{name='zhangsan', age=1}, Person{name='lisi', age=2}]
ArrayList<Integer> list = new ArrayList<>(integers);
System.out.println(list.getClass()+" "+list);//class java.util.ArrayList [1, 2, 3, 4]
copyOf 生成一个新的数组
調用了 System.arraycopy方法,长度取:Math.min(original.length, newLength)
【Java】System.arraycopy
public static int[] copyOf(int[] original, int newLength) {
int[] copy = new int[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
int[] ints = new int[]{1, 2, 3, 4};
int[] ints1 = Arrays.copyOf(ints, 8);
int[] ints2 = Arrays.copyOf(ints, 3);
System.out.println(Arrays.toString(ints));//[1, 2, 3, 4]
System.out.println(Arrays.toString(ints1));//[1, 2, 3, 4, 0, 0, 0, 0]
System.out.println(Arrays.toString(ints2));//[1, 2, 3]
Person[] people = new Person[]{new Person("zy1", 27), new Person("wxf1", 18)};
Person[] people1 = Arrays.copyOf(people, 4);
System.out.println(Arrays.toString(people));//[Person{name='zy1', age=27}, Person{name='wxf1', age=18}]
System.out.println(Arrays.toString(people1));//[Person{name='zy1', age=27}, Person{name='wxf1', age=18}, null, null]
sort 将指定的对象数组按照其元素的自然顺序升序排序
采用了快排
int[] ints = new int[]{4, 3, 2, 1};
System.out.println(Arrays.toString(ints));//[4, 3, 2, 1]
Arrays.sort(ints);
System.out.println(Arrays.toString(ints));//[1, 2, 3, 4]
long[] longs = new long[]{4, 3, 2, 1};
System.out.println(Arrays.toString(longs));//[4, 3, 2, 1]
Arrays.sort(longs, 1, 3);
System.out.println(Arrays.toString(longs));//[4, 2, 3, 1]
binarySearch 二分查找
int[] ints = new int[]{1, 2, 3, 4, 5, 10};
System.out.println(Arrays.binarySearch(ints, 10));//5
int[] ints1 = new int[]{1, 2, 3, 4, 5, 7, 8, 10, 100};
System.out.println(Arrays.binarySearch(ints1, 0, 5, 2));//1
hashCode+deepHashCode
O ( n ) O(n) O(n)
public static int hashCode(int a[]) {
if (a == null)
return 0;
int result = 1;
for (int element : a)
result = 31 * result + element;
return result;
}
deepHashCode中最终调用hashCode
public static int deepHashCode(Object a[]) {
if (a == null)
return 0;
int result = 1;
for (Object element : a) {
int elementHash = 0;
if (element instanceof Object[])
elementHash = deepHashCode((Object[]) element);
else if (element instanceof byte[])
elementHash = hashCode((byte[]) element);
else if (element instanceof short[])
elementHash = hashCode((short[]) element);
else if (element instanceof int[])
elementHash = hashCode((int[]) element);
else if (element instanceof long[])
elementHash = hashCode((long[]) element);
else if (element instanceof char[])
elementHash = hashCode((char[]) element);
else if (element instanceof float[])
elementHash = hashCode((float[]) element);
else if (element instanceof double[])
elementHash = hashCode((double[]) element);
else if (element instanceof boolean[])
elementHash = hashCode((boolean[]) element);
else if (element != null)
elementHash = element.hashCode();
result = 31 * result + elementHash;
}
return result;
}
int[] ints = new int[]{1, 2, 3, 4, 5, 10};
System.out.println(Arrays.hashCode(ints));//918073256
int[] ints1 = new int[]{1, 2, 3, 4, 5, 7, 8, 10, 100};
System.out.println(Arrays.hashCode(ints1));//-31452707