一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java經典用法總結

Java經典用法總結

2020-03-28 11:10lijiao JAVA教程

這篇文章主要介紹了Java經典用法總結,在本文中,盡量收集一些java最常用的習慣用法,特別是很難猜到的用法,感興趣的小伙伴們可以參考一下

在Java編程中,有些知識并不能僅通過語言規范或者標準API文檔就能學到的,本文為大家羅列。

一、實現

1、現equals() 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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() { ... }
 
}
  • 參數必須是Object類型,不能是外圍類。
  • foo.equals(null) 必須返回false,不能拋NullPointerException。(注意,null instanceof 任意類 總是返回false,因此上面的代碼可以運行。)
  • 基本類型域(比如,int)的比較使用 == ,基本類型數組域的比較使用Arrays.equals()。
  • 覆蓋equals()時,記得要相應地覆蓋 hashCode(),與 equals() 保持一致。

2、現hashCode()

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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) { ... }
 
}
  • 當x和y兩個對象具有x.equals(y) == true ,你必須要確保x.hashCode() == y.hashCode()。
  • 根據逆反命題,如果x.hashCode() != y.hashCode(),那么x.equals(y) == false 必定成立。
  • 你不需要保證,當x.equals(y) == false時,x.hashCode() != y.hashCode()。但是,如果你可以盡可能地使它成立的話,這會提高哈希表的性能。
  • hashCode()最簡單的合法實現就是簡單地return 0;雖然這個實現是正確的,但是這會導致HashMap這些數據結構運行得很慢。

3、實現compareTo() 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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()

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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);
 
  }
 
 }
 
}
  • 使用 super.clone() 讓Object類負責創建新的對象。
  • 基本類型域都已經被正確地復制了。同樣,我們不需要去克隆String和BigInteger等不可變類型。
  • 手動對所有的非基本類型域(對象和數組)進行深度復制(deep copy)。
  • 實現了Cloneable的類,clone()方法永遠不要拋CloneNotSupportedException。因此,需要捕獲這個異常并忽略它,或者使用不受檢異常(unchecked exception)包裝它。
  • 不使用Object.clone()方法而是手動地實現clone()方法是可以的也是合法的。

二、預防性檢測

1、預防性檢測(Defensive checking)數值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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、預防性檢測對象

?
1
2
3
4
5
6
7
8
9
int findIndex(List<String> list, String target) {
 
 if (list == null || target == null)
 
  throw new NullPointerException();
 
 ...
 
}
  • 不要認為對象參數不會為空(null)。要顯式地檢測這個條件。

3、預防性檢測數組索引

?
1
2
3
4
5
6
7
8
9
10
11
12
13
void frob(byte[] b, int index) {
 
 if (b == null)
 
  throw new NullPointerException();
 
 if (index < 0 || index >= b.length)
 
  throw new IndexOutOfBoundsException();
 
 ...
 
}

不要認為所以給的數組索引不會越界。要顯式地檢測它。

4、預防性檢測數組區間

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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、填充數組元素
使用循環: 

?
1
2
3
4
5
6
7
8
9
10
11
// 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、復制一個范圍內的數組元素
使用循環:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 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、調整數組大小
使用循環(擴大規模): 

?
1
2
3
4
5
6
7
8
9
10
11
// 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;

使用循環(減小規模):

?
1
2
3
4
5
6
// 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;

(優先)使用標準庫的方法:

?
1
1a = Arrays.copyOf(a, newLen);

4、把4個字節包裝(packing)成一個int

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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個字節 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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),不要使用算術右移操作符(>>)。

以上就是本文的全部內容,希望對大家的學習有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 毛片免费观看的视频 | 久久久精品日本一区二区三区 | 好男人免费高清在线观看2019 | 免费看视频高清在线观看 | 亚洲欧美专区 | 日韩毛片免费线上观看 | 日韩a级片视频 | 五月天国产精品 | 国产精品俺来也在线观看了 | 国产精品 视频一区 二区三区 | 天美影视文化传媒mv免费 | 精品99一区二区三区麻豆 | 四虎小视频| 亚洲经典激情春色另类 | 亚洲精品6久久久久中文字幕 | 女人和男人搞基 | 91麻豆国产福利精品 | 91精品国产麻豆国产自产在线 | 女教师巨大乳孔中文字幕免费 | 俄罗斯15一16处交 | 欧美精品久久久久久久免费观看 | 短篇艳妇系列 | 免费高清在线观看 | 嫩草在线视频www免费观看 | 精品在线免费观看 | 91李宗精品72集在线观看 | 精新精新国产自在现拍 | 5月色婷婷| 亚洲欧美综合人成野草 | 俄罗斯女人与公拘i交酡 | 亚洲AV无码国产精品色午夜情 | 男女拍拍拍免费视频网站 | 齐天大性之七仙女欲春迅雷链接 | 国产成人在线视频播放 | 第四色男人天堂 | 精品一区二区三区 不卡高清 | 97蝌蚪自拍自窝 | www射com| 国产成人精品实拍在线 | 2022超帅男同gayxxx | 俄罗斯图书馆无打码久久 |