DecimalFormat f = new DecimalFormat("$#,##0.00;-$#,##0.00");
System.out.println(f.format(new BigDecimal(-12345)));
//-$12,345.00
Author Archives: mmommo
JavaFX – 不让 Label 出现省略
label.setMinSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);
Java HashMap 遍历
只要键名, key
Map
for (String key : map.keySet()) {
// ...
}
只要键值, value
for (Object value : map.values()) {
// ...
}
两个都要, key & value
for (Map.Entry
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
还有复杂点儿的:
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
}
JAVA List 类型转换 (Casting List>)
public class TypeA {}
public class TypeB extends TestA{}
我要转换:
List A = new List<TypeA>();
List B = (List<TypeB>)A;
错误:”inconvertible types”。怎么办??
List B = (List<TypeB>)(List<?>)A;
好吧,没错误了。
纯属欺骗,谨慎使用。
相关知识阅读(英语)Wildcards and Subtyping