- for循环和迭代器
for (Iterator i = destinations.iterator(); i.hasNext();) {}
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(arr));
- 在判断一个数组中是否包含某个值的时候,开发者经常这样做:
Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);
推荐使用for循环遍历的形式或者使用Apache Commons类库中提供的ArrayUtils类的contains方法。
思考
public static void add(List list, Object o){
list.add(o);
}
public static void main(String[] args){
List<String> list = new ArrayList<String>();
add(list, 10);
String s = list.get(0);
}
ArrayList与LinkedList的选择
简而言之,如果有大量的增加删除操作并且没有很多的随机访问元素的操作,应该首先LinkedList。
- ByteBuffer 存取无符号数值
public static short getUnsignedByte(ByteBuffer buff) {
return (short) (buff.get() & 0xff);
}
public static short getUnsignedByte(ByteBuffer buff, int position) {
return (short) (buff.get(position) & (short) 0xff);
}
public static void putUnsignedByte(ByteBuffer buff, int value) {
buff.put((byte) (value & 0xff));
}
public static void putUnsignedByte(ByteBuffer buff, int position, int value) {
buff.put(position, (byte) (value & 0xff));
}
// --------------------------------------------
public static int getUnsignedShort(ByteBuffer buff) {
return buff.getShort() & 0xffff;
}
public static int getUnsignedShort(ByteBuffer buff, int position) {
return buff.getShort(position) & (short) 0xffff;
}
public static void putUnsignedShort(ByteBuffer buff, int value) {
buff.putShort((short) (value & 0xffff));
}
public static void putUnsignedShort(ByteBuffer buff, int position, int value) {
buff.putShort(position, (short) (value & 0xffff));
}
// --------------------------------------------
public static long getUnsignedInt(ByteBuffer buff) {
return buff.getInt() & 0xffffffffL;
}
public static long getUnsignedInt(ByteBuffer buff, int position) {
return buff.getInt(position) & 0xffffffffL;
}
public static void putUnsignedInt(ByteBuffer buff, int value) {
buff.putInt((int) (value & 0xffffffffL));
}
public static void putUnsignedInt(ByteBuffer buff, int position, int value) {
buff.putInt(position, (int) (value & 0xffff));
}
NOTE: 原文链接:https://blog.csdn.net/abc_key/article/details/31376901