item 28 JihoonKim - JAVA-JIKIMI/EFFECTIVE-JAVA3 GitHub Wiki
-
λ°°μ΄μ
곡λ³('ν¨κ» λ³νλ€')μ΄μ§λ§, μ λ€λ¦μλΆκ³΅λ³μ΄λ€.-
Subκ°Superμ νμ νμ μ΄λ©΄,Sub[]λSuper[]μ νμ νμ μ΄λ€.Object[] objectArray = new Long[1]; // Longμ΄ Objectμ νμ νμ objectArray[0] = "Longμ Stringμ λ£μ μ μμ΄"; // λ°νμμ ArrayStoreException λ°μ
- μλ‘ λ€λ₯Έ νμ
Type1,Type2κ° μμ λ,List<Type1>μList<Type2>μ μμ νμ λ, νμ νμ λ μλλ€.(JLS-4.10)List<Object> ol = new ArrayList<Long>(); // νΈνλμ§ μλ νμ , Compile μλ¬ ol.add("Longμ Stringμ λ£μ μ μμ΄");
-
-
λ°°μ΄μ
μ€μ²΄ν(reify)λλ€. (JLS-4.7)- λ°°μ΄μ λ°νμμλ μμ μ΄ λ΄κΈ°λ‘ ν μμμ νμ
μ μΈμ§νκ³ νμΈνλ€λ κ²
-
ArrayStoreExceptionλ°μλ κ°λ₯- μ λ€λ¦μ νμ
μ λ³΄κ° λ°νμμλ
μκ±°(erasure)λλ€.(JLS-4.6)- μμ νμ μ μ»΄νμΌνμμλ§ κ²μ¬νλ©° λ°νμμλ μ μ μ‘°μ°¨ μλ€
- μ λ€λ¦ μ§μ μ λ κ±°μ μ½λ λν νμ νΈνμ± λͺ©μ
- μ λ€λ¦μ νμ
μ λ³΄κ° λ°νμμλ
- λ°°μ΄μ λ°νμμλ μμ μ΄ λ΄κΈ°λ‘ ν μμμ νμ
μ μΈμ§νκ³ νμΈνλ€λ κ²
-
-
new List<E>[](μ λ€λ¦ νμ ),new List<String>[](λ§€κ°λ³μν νμ ),new E[](νμ λ§€κ°λ³μ) μμΌλ‘ μμ±νλ©΄ μ»΄νμΌ μλ¬ λ°μνλ€. - κ·Έ μ΄μ λ νμ
μμ νμ§ μκΈ° λλ¬Έμ΄λ€.
- νμ©νλ€λ©΄ μ»΄νμΌλ¬κ° μλ μμ±ν νλ³ν μ½λμμ λ°νμμ
ClassCastExceptionμ΄ λ°μν μ μμ΄, μ΄λ μ λ€λ¦ νμ μμ€ν μ μ·¨μ§μ μ΄κΈλλ€.List<String>[] stringLists = new List<String>[1]; // (1), μ»΄νμΌ μλ¬ λ°μ, νμ§λ§ νμ©λλ€κ³ κ°μ List<Integer> intList = List.of(42); // (2), List.of()λ JDK 9λΆν° μ¬μ© κ°λ₯ Object[] objects = stringLists; // (3) objects[0] = intList; // (4) String s = stringLists[0].get(0); // (5), λ¬Έμ λ°μ
- λ°°μ΄μ 곡λ³μ΄λ, (3)μ (1)μμ μμ±ν
List<String>μ λ°°μ΄μObjectλ°°μ΄μ ν λΉνλ κ²μ΄ κ°λ₯νλ€. - μ λ€λ¦μ μκ±° λ°©μμ΄λ, λ°νμμ
List<Integer>μΈμ€ν΄μ€μ νμ μ λ¨μListκ° λκ³ ,List<Integer>[]μList<String>μΈμ€ν΄μ€μ νμ μList[]κ° λλ€. κ·Έλμ, (4)μμλ (2)μμ μμ±νList<Integer>μ μΈμ€ν΄μ€λ₯ΌObjectλ°°μ΄μ 첫 μμλ‘ μ μ₯ν΄λArrayStoreExceptionμ΄ λ°μνμ§ μλλ€ - λ¬Έμ λ (5)μμ λ°μνλ€.
List<String>μΈμ€ν΄μ€λ§ λ΄κ² λ€κ³ μ μΈν stringLists λ°°μ΄μ λ€λ₯Έ νμ μ μΈμ€ν΄μ€(List<Integer>)κ° λ΄κ²¨μλλ°, 첫 μμλ₯Ό κΊΌλ΄λ €κ³ νλ€. κ·Έλ¦¬κ³ μ΄λ₯Ό StringμΌλ‘ νλ³ννλλ°, μ΄ μμλ Integer νμ μ΄λ―λ‘ λ°νμμClassCastExceptionμ΄ λ°μ
- λ°°μ΄μ 곡λ³μ΄λ, (3)μ (1)μμ μμ±ν
- νμ©νλ€λ©΄ μ»΄νμΌλ¬κ° μλ μμ±ν νλ³ν μ½λμμ λ°νμμ
-
E,List<E>,List<String>κ°μ νμ μμ€μ²΄ν λΆκ° νμ(non-reifiable type, JLS-4.7)μ΄λ€.- μ»΄νμΌνμλ³΄λ€ λ°νμμ νμ μ 보λ₯Ό μ κ² κ°λλ€
-
μκ±°λ©μ»€λμ¦μΌλ‘ μΈν΄ λ§€κ°λ³μν νμ κ°μ΄λ° μ€μ²΄νλ μ μλ νμ μList<?>μMap<?,?>κ°μ λΉνμ μ μμΌλμΉ΄λ νμ λΏ(item26)
μ λ€λ¦ λ°°μ΄ μμ± μ€λ₯λ λΉκ²μ¬ νλ³ν κ²½κ³ κ° λ¨λ κ²½μ° λλΆλΆ λ°°μ΄μΈ E[] λμ 컬λ μ
μΈ List<E>λ₯Ό μ¬μ©νλ©΄ ν΄κ²°λλ€.
// μ λ€λ¦ μ°μ§ μκ³ κ΅¬ν
public class Chooser {
public final Object[] choiceArray;
public Chooser(Collection choices) {
choiceArray = choices.toArray();
}
// μ΄ λ©μλλ₯Ό μ¬μ©νλ κ³³μμλ λ§€λ² νλ³νμ΄ νμ
// λ°νμμ νλ³ν μ€λ₯μ κ°λ₯μ±
public Object choose() {
Random rnd = ThreadLocalRandom.current();
return choiceArray[rnd.nextInt(choiceArray.length)];
}
}// μ λ€λ¦ ꡬνμ μν μλ(μ»΄νμΌ μλ¬, κ²½κ³ λ°μ)
public class Chooser<T> {
public final T[] choiceArray;
public Chooser(Collection<T> choices) {
// [1] μ»΄νμΌ μλ¬, incompatible types: java.lang.Object[] cannot be converted to T[]
choiceArray = choices.toArray();
// [2] 'Unchecked Cast' κ²½κ³ , Tμ νμ
μ λͺ°λΌ μ΄ νλ³νμ΄ λ°νμμλ μμ νμ§ λ³΄μ₯ν μ μλ€.
choiceArray = (T[]) choices.toArray();
}
// choose methodλ λμΌ
}// 리μ€νΈ κΈ°λ° Chooser (νμ
μμ μ± ν보)
public class Chooser<T> {
public final List<T> choiceList;
public Chooser(Collection<T> choices) {
choiceList = new ArrayList<>(choices);
}
public T choose() {
Random rnd = ThreadLocalRandom.current();
return choiceList.get(rnd.nextInt(choiceList.size()));
}
}- λ°°μ΄κ³Ό μ λ€λ¦μ λ§€μ° λ€λ₯Έ κ·μΉμ΄ μ μ©λλ€.
- λ°°μ΄μ '곡λ³'κ³Ό 'μ€μ²΄ν'(λ°νμμ νμ μμ )
- μ λ€λ¦μ 'λΆκ³΅λ³'κ³Ό νμ μ 보 'μκ±°'(μ»΄νμΌ νμμ νμ μμ )
- λμ μμ΄ μ°λ€ μ»΄νμΌ μ€λ₯λ κ²½κ³ λ₯Ό λ§λλ©΄, κ°μ₯ λ¨Όμ λ°°μ΄μ 리μ€νΈλ‘ λ체ν΄λ³΄μ!