激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - JAVA教程 - Java經(jīng)典用法總結(jié)

Java經(jīng)典用法總結(jié)

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

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

在Java編程中,有些知識并不能僅通過語言規(guī)范或者標(biāo)準(zhǔn)API文檔就能學(xué)到的,本文為大家羅列。

一、實(shí)現(xiàn)

1、現(xiàn)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() { ... }
 
}
  • 參數(shù)必須是Object類型,不能是外圍類。
  • foo.equals(null) 必須返回false,不能拋NullPointerException。(注意,null instanceof 任意類 總是返回false,因此上面的代碼可以運(yùn)行。)
  • 基本類型域(比如,int)的比較使用 == ,基本類型數(shù)組域的比較使用Arrays.equals()。
  • 覆蓋equals()時,記得要相應(yīng)地覆蓋 hashCode(),與 equals() 保持一致。

2、現(xiàn)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) { ... }
 
}
  • 當(dāng)x和y兩個對象具有x.equals(y) == true ,你必須要確保x.hashCode() == y.hashCode()。
  • 根據(jù)逆反命題,如果x.hashCode() != y.hashCode(),那么x.equals(y) == false 必定成立。
  • 你不需要保證,當(dāng)x.equals(y) == false時,x.hashCode() != y.hashCode()。但是,如果你可以盡可能地使它成立的話,這會提高哈希表的性能。
  • hashCode()最簡單的合法實(shí)現(xiàn)就是簡單地return 0;雖然這個實(shí)現(xiàn)是正確的,但是這會導(dǎo)致HashMap這些數(shù)據(jù)結(jié)構(gòu)運(yùn)行得很慢。

3、實(shí)現(xiàn)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;
 
 }
 
}

總是實(shí)現(xiàn)泛型版本 Comparable 而不是實(shí)現(xiàn)原始類型 Comparable 。因?yàn)檫@樣可以節(jié)省代碼量和減少不必要的麻煩。
只關(guān)心返回結(jié)果的正負(fù)號(負(fù)/零/正),它們的大小不重要。
Comparator.compare()的實(shí)現(xiàn)與這個類似。

4、實(shí)現(xiàn)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類負(fù)責(zé)創(chuàng)建新的對象。
  • 基本類型域都已經(jīng)被正確地復(fù)制了。同樣,我們不需要去克隆String和BigInteger等不可變類型。
  • 手動對所有的非基本類型域(對象和數(shù)組)進(jìn)行深度復(fù)制(deep copy)。
  • 實(shí)現(xiàn)了Cloneable的類,clone()方法永遠(yuǎn)不要拋CloneNotSupportedException。因此,需要捕獲這個異常并忽略它,或者使用不受檢異常(unchecked exception)包裝它。
  • 不使用Object.clone()方法而是手動地實(shí)現(xiàn)clone()方法是可以的也是合法的。

二、預(yù)防性檢測

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

?
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);
 
}
  • 不要認(rèn)為輸入的數(shù)值都是正數(shù)、足夠小的數(shù)等等。要顯式地檢測這些條件。
  • 一個設(shè)計良好的函數(shù)應(yīng)該對所有可能性的輸入值都能夠正確地執(zhí)行。要確保所有的情況都考慮到了并且不會產(chǎn)生錯誤的輸出(比如溢出)。

2、預(yù)防性檢測對象

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

3、預(yù)防性檢測數(shù)組索引

?
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();
 
 ...
 
}

不要認(rèn)為所以給的數(shù)組索引不會越界。要顯式地檢測它。

4、預(yù)防性檢測數(shù)組區(qū)間

?
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();
 
 ...
 
}

不要認(rèn)為所給的數(shù)組區(qū)間(比如,從off開始,讀取len個元素)是不會越界。要顯式地檢測它。

三、數(shù)組

1、填充數(shù)組元素
使用循環(huán): 

?
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;
 
(優(yōu)先)使用標(biāo)準(zhǔn)庫的方法:
 
Arrays.fill(a, (byte)123);

2、復(fù)制一個范圍內(nèi)的數(shù)組元素
使用循環(huán):

?
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];
 
(優(yōu)先)使用標(biāo)準(zhǔn)庫的方法:
 
System.arraycopy(a, 3, b, 6, 8);

3、調(diào)整數(shù)組大小
使用循環(huán)(擴(kuò)大規(guī)模): 

?
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;

使用循環(huán)(減小規(guī)模):

?
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;

(優(yōu)先)使用標(biāo)準(zhǔn)庫的方法:

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

4、把4個字節(jié)包裝(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個字節(jié) 

 

?
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)
 
 };
 
}

總是使用無符號右移操作符(>>>)對位進(jìn)行包裝(packing),不要使用算術(shù)右移操作符(>>)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲男人的天堂在线视频 | 国产亚洲精品成人a | 国产成年人视频 | 久草在线观看资源 | 国产资源在线观看视频 | 日本在线观看视频网站 | 日本成人一二三区 | 怦然心动50免费完整版 | 精品一区二区在线播放 | 国产一级大片在线观看 | 午夜一级 | 五月天堂婷婷 | 日韩欧美精品中文字幕 | hd porn 4k video xhicial| 精品亚洲二区 | 亚洲一区二区三区在线看 | 午夜在线小视频 | 午夜一级 | 日本免费aaa观看 | 日韩区在线| 国产午夜精品视频免费不卡69堂 | 免费黄网站在线播放 | 经典三级在线视频 | 精品一区二区免费 | 欧产日产国产精品乱噜噜 | 一区二区三级视频 | 成人福利视频在线观看 | 国产精品成人一区二区三区电影毛片 | 国产成人高清在线观看 | 亚洲第一页综合 | av免费在线观 | 看一级毛片 | 亚洲少妇诱惑 | 日韩精品a在线观看 | 欧日一级片| 国产精品爱久久久久久久 | 一级毛片真人免费播放视频 | 久久久久性 | 日韩色电影 | 久草手机在线视频 | 色a综合 |