在Java编程中,有些知识并不能仅通过语言规范或者标准API文档就能学到的,本文为大家罗列。
一、实现
1、现equals()
class Person {
String name;
int birthYear;
byte[] raw;
public boolean equals(Object obj) {
if (!obj instanceof Person)
return false;
Person other = (Person)obj;
return name.equals(other.name)
&& birthYear == other.birthYear
&& Arrays.equals(raw, other.raw);
}
public int hashCode() { ... }
}
2、现hashCode()
class Person {
String a;
Object b;
byte c;
int[] d;
public int hashCode() {
return a.hashCode() + b.hashCode() + c + Arrays.hashCode(d);
}
public boolean equals(Object o) { ... }
}
3、实现compareTo()
class Person implements Comparable<Person> {
String firstName;
String lastName;
int birthdate;
// Compare by firstName, break ties by lastName, finally break ties by birthdate
public int compareTo(Person other) {
if (firstName.compareTo(other.firstName) != 0)
return firstName.compareTo(other.firstName);
else if (lastName.compareTo(other.lastName) != 0)
return lastName.compareTo(other.lastName);
else if (birthdate < other.birthdate)
return -1;
else if (birthdate > other.birthdate)
return 1;
else
return 0;
}
}
总是实现泛型版本 Comparable 而不是实现原始类型 Comparable 。因为这样可以节省代码量和减少不必要的麻烦。
只关心返回结果的正负号(负/零/正),它们的大小不重要。
Comparator.compare()的实现与这个类似。
4、实现clone()
class Values implements Cloneable {
String abc;
double foo;
int[] bars;
Date hired;
public Values clone() {
try {
Values result = (Values)super.clone();
result.bars = result.bars.clone();
result.hired = result.hired.clone();
return result;
} catch (CloneNotSupportedException e) { // Impossible
throw new AssertionError(e);
}
}
}
二、预防性检测
1、预防性检测(Defensive checking)数值
int factorial(int n) {
if (n < 0)
throw new IllegalArgumentException(\"Undefined\");
else if (n >= 13)
throw new ArithmeticException(\"Result overflow\");
else if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
2、预防性检测对象
int findIndex(List<String> list, String target) {
if (list == null || target == null)
throw new NullPointerException();
...
}
3、预防性检测数组索引
void frob(byte[] b, int index) {
if (b == null)
throw new NullPointerException();
if (index < 0 || index >= b.length)
throw new IndexOutOfBoundsException();
...
}
不要认为所以给的数组索引不会越界。要显式地检测它。
4、预防性检测数组区间
void frob(byte[] b, int off, int len) {
if (b == null)
throw new NullPointerException();
if (off < 0 || off > b.length
|| len < 0 || b.length - off < len)
throw new IndexOutOfBoundsException();
...
}
不要认为所给的数组区间(比如,从off开始,读取len个元素)是不会越界。要显式地检测它。
三、数组
1、填充数组元素
使用循环:
// Fill each element of array \'a\' with 123 byte[] a = (...); for (int i = 0; i < a.length; i++) a[i] = 123; (优先)使用标准库的方法: Arrays.fill(a, (byte)123);
2、复制一个范围内的数组元素
使用循环:
// Copy 8 elements from array \'a\' starting at offset 3 // to array \'b\' starting at offset 6, // assuming \'a\' and \'b\' are distinct arrays byte[] a = (...); byte[] b = (...); for (int i = 0; i < 8; i++) b[6 + i] = a[3 + i]; (优先)使用标准库的方法: System.arraycopy(a, 3, b, 6, 8);
3、调整数组大小
使用循环(扩大规模):
// Make array \'a\' larger to newLen byte[] a = (...); byte[] b = new byte[newLen]; for (int i = 0; i < a.length; i++) // Goes up to length of A b[i] = a[i]; a = b;
使用循环(减小规模):
// Make array \'a\' smaller to newLen byte[] a = (...); byte[] b = new byte[newLen]; for (int i = 0; i < b.length; i++) // Goes up to length of B b[i] = a[i]; a = b;
(优先)使用标准库的方法:
1a = Arrays.copyOf(a, newLen);
4、把4个字节包装(packing)成一个int
int packBigEndian(byte[] b) {
return (b[0] & 0xFF) << 24
| (b[1] & 0xFF) << 16
| (b[2] & 0xFF) << 8
| (b[3] & 0xFF) << 0;
}
int packLittleEndian(byte[] b) {
return (b[0] & 0xFF) << 0
| (b[1] & 0xFF) << 8
| (b[2] & 0xFF) << 16
| (b[3] & 0xFF) << 24;
}
5、把int分解(Unpacking)成4个字节
byte[] unpackBigEndian(int x) {
return new byte[] {
(byte)(x >>> 24),
(byte)(x >>> 16),
(byte)(x >>> 8),
(byte)(x >>> 0)
};
}
byte[] unpackLittleEndian(int x) {
return new byte[] {
(byte)(x >>> 0),
(byte)(x >>> 8),
(byte)(x >>> 16),
(byte)(x >>> 24)
};
}
总是使用无符号右移操作符(>>>)对位进行包装(packing),不要使用算术右移操作符(>>)。
以上就是本文的全部内容,希望对大家的学习有所帮助。
本文地址:https://www.stayed.cn/item/24760
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我