privatestaticint[] countingSort(int[] arr) { // 遍历数组,得到最值 int max = arr[0]; int min = arr[0]; for (int j : arr) { if (max < j) { max = j; } if (min > j) { min = j; } }
// 根据最值新建一个用于计数的数组, 比如原数组最大值为 4,最小值为 2,新建的计数数组为 [2,3,4],所以声明长度的时候为 max - min + 1 int[] bucket = newint[max - min + 1]; // 再次遍历数组,将对应的 bucket 下标做 ++ 操作 for (int i : arr) { bucket[i - min]++; } System.out.printf("%-10s: %s%n", "Bucket", Arrays.toString(bucket));
// 新建一个数组存储下标结束的值,用于保证排序稳定性 int[] endIndex = newint[bucket.length]; int sum = 0; for (int i = 0; i < endIndex.length; i++) { sum += bucket[i]; endIndex[i] = sum; } System.out.printf("%-10s: %s%n", "endIndex", Arrays.toString(endIndex));
privatestaticint[] countingSort(int[] arr) { // 遍历数组,得到最值 int max = arr[0]; int min = arr[0]; for (int j : arr) { if (max < j) { max = j; } if (min > j) { min = j; } }
// 根据最值新建一个用于计数的数组, 比如原数组最大值为 4,最小值为 2,新建的计数数组为 [2,3,4],所以声明长度的时候为 max - min + 1 int[] bucket = newint[max - min + 1]; // 再次遍历数组,将对应的 bucket 下标做 ++ 操作 for (int i : arr) { bucket[i - min]++; } System.out.printf("%-10s: %s%n", "Bucket", Arrays.toString(bucket));
// 新建一个数组存储下标结束的值,用于保证排序稳定性 for (int i = 1; i < bucket.length; i++) { bucket[i] = bucket[i] + bucket[i - 1]; } System.out.printf("%-10s: %s%n", "Index", Arrays.toString(bucket));
.zprofile is basically the same as .zlogin except that it’s sourced before .zshrc while .zlogin is sourced after .zshrc. According to the zsh documentation, “.zprofile is meant as an alternative to .zlogin for ksh fans; the two are not intended to be used together, although this could certainly be done if desired.”
今天在查找某个 bug 的 root cause 的时候,发现一个 Class.getEnclosingClass() 的调用。从来没有用个这玩意儿,做下笔记。
定义
人话就是:返回这个类的外部类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * Returns the immediately enclosing class of the underlying * class. If the underlying class is a top level class this * method returns {@code null}. * @return the immediately enclosing class of the underlying class * @exception SecurityException * If a security manager, <i>s</i>, is present and the caller's * class loader is not the same as or an ancestor of the class * loader for the enclosing class and invocation of {@link * SecurityManager#checkPackageAccess s.checkPackageAccess()} * denies access to the package of the enclosing class * @since 1.5 */ @CallerSensitive public Class<?> getEnclosingClass() throws SecurityException
案件重现
以前有一段 code 组织形式如下,定义了一个 MyOuterClass,它有一个 field 动态实现了 MyAbsClass 这个抽象类。那么,当我们调用 MyOuterClass 实例的 field.getClass().getEcloseingClass() 的时候会返回外部类的 class name
classMyOuterClass{ MyAbsClass field = new MyAbsClass() {}; } // MyOuterClass
然后有个德国的 team 将这段代码重构了,形式如下。他们新建了一个类,继承了抽象类,并将原先的类成员变量声明的地方用这个新建类代替了。这种情况下,getEnclosingClass() 的调用方就变为一个 top level class 了,返回 null,随之导致 getSimpleName() 抛出 NPE 了。
classMyOuterClass{ MyAbsClass field = new MyAbsClassIns(); } // Exception in thread "main" java.lang.NullPointerException // at reading.container.Test.main(Test.java:5)
publicclassCloneTest{ publicstaticvoidmain(String[] args){ Person person = new Person(1, "jack"); System.out.println(person); Person clone = person.clone(); System.out.println(clone); } }
classPersonimplementsCloneable{ int age; String name;
// constructor + toString
@Override public Person clone(){ Person clone = null; try { clone = (Person)super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return clone; } }
publicclassCloneTest{ publicstaticvoidmain(String[] args)throws CloneNotSupportedException { Person person = new Person(1, "jack"); Address addr = new Address("shanghai"); person.addr = addr;
Person clone = person.clone(); addr.addr = "hangzhou";
publicclassCloneTest{ publicstaticvoidmain(String[] args)throws CloneNotSupportedException { Person person = new Person(1, "jack"); Address addr = new Address("shanghai"); person.addr = addr;
Person clone = person.clone(); addr.addr = "hangzhou";
## getset 先 get 再 set 127.0.0.1:6379> getset db redis # 如果不存在 返回 nil (nil) 127.0.0.1:6379> get db "redis" 127.0.0.1:6379> getset db mongodb # 如果存在,获取并更新 "redis" 127.0.0.1:6379> get db "mongodb"