Java經典用法總結。本站提示廣大學習愛好者:(Java經典用法總結)文章只能為提供參考,不一定能成為您想要的結果。以下是Java經典用法總結正文
在Java編程中,有些常識其實不能僅經由過程說話標准或許尺度API文檔就可以學到的,本文為年夜家枚舉。
1、完成
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);
}
}
}
2、預防性檢測
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個元素)是不會越界。要顯式地檢測它。
3、數組
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),不要應用算術右移操作符(>>)。
以上就是本文的全體內容,願望對年夜家的進修有所贊助。