public static String toString(int i, int radix) { if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) radix = 10;
/* Use the faster version */ if (radix == 10) { return toString(i); } // int 32位 char buf[] = new char[33]; boolean negative = (i < 0); int charPos = 32;
if (!negative) { i = -i; } // 根据进制取余转换 while (i <= -radix) { buf[charPos--] = digits[-(i % radix)]; i = i / radix; } buf[charPos] = digits[-i];
if (negative) { buf[--charPos] = '-'; }
return new String(buf, charPos, (33 - charPos)); }
不过该方法需注意:If the first argument is negative, the first element of the result is the ASCII minus character '-' ('\u005Cu002D'). If the first argument is not negative, no sign character appears in the result. 例如:
public static Integer decode(String nm) throws NumberFormatException { int radix = 10; int index = 0; boolean negative = false; Integer result;
if (nm.length() == 0) throw new NumberFormatException("Zero length string"); char firstChar = nm.charAt(0); // Handle sign, if present if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') index++;
// Handle radix specifier, if present if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { index += 2; radix = 16; } else if (nm.startsWith("#", index)) { index ++; radix = 16; } else if (nm.startsWith("0", index) && nm.length() > 1 + index) { // 0 后面长度要大于 1 index ++; radix = 8; }
if (nm.startsWith("-", index) || nm.startsWith("+", index)) throw new NumberFormatException("Sign character in wrong position");
try { result = Integer.valueOf(nm.substring(index), radix); result = negative ? Integer.valueOf(-result.intValue()) : result; } catch (NumberFormatException e) { // If number is Integer.MIN_VALUE, we'll end up here. The next line // handles this case, and causes any genuine format error to be // rethrown. String constant = negative ? ("-" + nm.substring(index)) : nm.substring(index); result = Integer.valueOf(constant, radix); } return result; }
int n = 5;// 5件物品,物品编号为a,b,c,d,e(下面为多加一件物品,第一个物品为虚拟的物品) int weight[] = { 0, 4, 5, 6, 2, 2 };// 物品的重量 int value[] = { 0, 6, 4, 5, 3, 6 }; // 对应物品的价值 int c = 10; // 背包容量 int state[] = { 0, 0, 0, 0, 0, 0 };// 开始状态 char name[] = { ' ', 'a', 'b', 'c', 'd', 'e' }; int maxValue = getMaxValue(n, weight, value, state, c); System.out.println("最大价值为 = " + maxValue); System.out.print("放入的物品为 :"); for (int i = 1; i <= 5; i++) { if (state[i] == 1) { System.out.print(name[i] + " "); } }
// System.out.println(); }
/** * * @param n * 物品数量 * @param weight * 物品对应重量(数组下标从0开始,故第一个物品为虚拟物品) * @param value * 物品对应价值(数组下标从0开始,故第一个物品为虚拟物品) * @param state * 物品的开始状态 * @param c * 背包的容量 * @return */ public static int getMaxValue(int n, int weight[], int value[], int state[], int c) { // n 为物品的数量,数组时需要加 1,此时可以从 0,1,...n 个物品,共 n+1 个商品,其中第 0 个为虚构物品 // 对于物品的价值,可以写成 2 维数组 int m[][] = new int[n + 1][c + 1]; // n 为 0,1,2...(n-1),背包重量为 0,1,2...C int i, j;
for (i = 0; i <= n; i++) { m[i][0] = 0; } for (j = 0; j <= c; j++) { m[0][j] = 0; }
居住于 A 城的伯父,沉沦于二十年右派生涯,早妻离子散,平反后已垂垂暮老,多回忆早年英武及故友。我以他大学的一位女生名义去信慰藉,不想他立即复信,只好信来信往,谈当年的友情,谈数十年的思念,谈现在鳏寡人的处境,及至发展到黄昏恋。我半月一封,连续四年不断,且信中一再说要去见他,每次日期将至又以患病推延。伯父终老弱病倒,我去看他,临咽气说:“我等不及她来了。她来了,你把这个箱子交她。”又说一句:“我总没白活。”安详瞑目。掩埋了伯父,打开箱子,竟是我写给他的近百封信,得意为他在爱的幸福中度过晚年,不禁乐而开笑。
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD}) public @interface Bogus { ... }
@Retention
定义了该 Annotation 被保留的时间长短:某些 Annotation 仅出现在源代码中,而被编译器丢弃;而另一些却被编译在 class 文件中;编译在 class 文件中的 Annotation 可能会被虚拟机忽略,而另一些在 class 被装载时将被读取(请注意并不影响 class 的执行,因为 Annotation 与 class 在使用上是被分离的)。使用这个 meta-Annotation 可以对 Annotation 的“生命周期”限制。来源于 java.lang.annotation.RetentionPolicy 的枚举类型值:
在运行测试之前,我先将原先Teacher中的age字段属性类型稍稍修改了下。改为了private String age,结果为Could not copy properties from source to target; nested exception is java.lang.IllegalArgumentException,抛了个异常错误。而后我将其类型改回,输出Teacher :[18 ,180 ,男]。故:拷贝的目标数组与源数组中的元素对象其属性名称一样,类型就必须一致,否则会报错
一样,在运行测试之前,我先将原先Teacher中的age字段属性类型稍稍修改了下。改为了private String age,结果显示为:Cannot invoke com.tonglei.test.Teacher.setAge on bean class 'class com.tonglei.test.Teacher' - argument type mismatch - had objects of type "java.lang.Integer" but expected signature "java.lang.String",报错抛出类型不匹配异常信息,再将类型改回,输出:
该方法签名为void forEach(BiConsumer<? super K, ? super V> action),作用是对Map中每个映射执行action指定的操作,其中BiConsumer是一个函数接口,里面有一个待实现方法void accept(T t, U u)。BinConsumer接口名字和accept()方法名字都不重要,不须记住。
在Java7及以前,要想替换Map中的映射关系可通过put(K key, V value)方法来实现,该方法总是会用新值替换原来的值,为了更精确的控制替换行为,Java8在Map中加入了两个replace()方法,分别如下:
replace(K key, V value),只有在当前Map中**key的映射存在时**才用value去替换原来的值,否则什么也不做。
replace(K key, V oldValue, V newValue),只有在当前Map中**key的映射存在且等于oldValue时**才用newValue去替换原来的值,否则什么也不做。
replaceAll()
该方法签名为replaceAll(BiFunction<? super K, ? super V, ? extends V> function),作用是对Map中的每个映射执行function指定的操作,并用function的执行结果替换原来的value,其中BiFunction是一个函数接口,里面有一个待实现方法R apply(T t, U u),不要被如此多的函数接口吓到,因为使用的时候根本不需要知道他们的名字。
/** * 快速排序 * * @param arr * @param lo * @param hi * @return */ public static int[] quickSort(int[] arr, int lo, int hi) { if (lo >= hi) return arr; int num = partition(arr, lo, hi); quickSort(arr, lo, num - 1); quickSort(arr, num + 1, hi); return arr; }
/** * 排序数组 返回基准值 * * @param arr * @param lo * @param hi * @return */ public static int partition(int[] arr, int lo, int hi) { int num = arr[lo]; if (lo >= hi) return 0; while (lo < hi) { while (hi > lo && arr[hi] >= num) { hi--; } arr[lo] = arr[hi]; while (hi > lo && arr[lo] <= num) { lo++; } arr[hi] = arr[lo]; } arr[hi] = num; return hi; }