1. λ°°μ΄κ³Ό μ λ€λ¦ νμ μ μ°¨μ΄ π¦
π 1. λ°°μ΄μ 곡λ³μ΄κ³ , μ λ€λ¦μ λΆκ³΅λ³μ΄λ€.
- λ°°μ΄μ 곡λ³(Convariant)μ΄λ€.
Subκ° Superμ νμ νμ μ΄λΌλ©΄ Sub[]λ λ°°μ΄ Super[]μ νμ νμ μ΄λ€. - μ λ€λ¦μ λΆκ³΅λ³(Invariant)μ΄λ€.
μλ‘ λ€λ₯Έ νμ Type1κ³Ό Type2κ° μμ λ, List<Type1>μ List<Type2>μ νμ νμ λ μλκ³ μμ νμ λ μλλ€.
곡λ³κ³Ό λΆκ³΅λ³ μ€ μ΄λ μͺ½μ΄ λ¬Έμ κ° μμκΉ? μ½λλ₯Ό ν΅ν΄ μμ보μ.
public static void main(String[] args) {
// λ°°μ΄
Object[] objectArray = new Long[1];
objectArray[0] = "νμ
μ΄ λ¬λΌμ λ£μ μ μλ€."; // ArrayStoreException (λ°νμμ λ°μ)
// μ λ€λ¦
List<Object> objectList = new ArrayList<Long>(); // νΈνλμ§ μλ νμ
μ΄λ€. (μ»΄νμΌμ λ°μ)
objectList.add("νμ
μ΄ λ¬λΌμ λ£μ μ μλ€.");
}
λ°°μ΄κ³Ό μ λ€λ¦ λͺ¨λ Longμ© μ μ₯μμ Stringμ λ£μ μ μλ€. λ€λ§ λ°°μ΄μμλ κ·Έ μ€μλ₯Ό λ°νμμ μκ² λμ§λ§, 리μ€νΈλ₯Ό μ¬μ©νλ©΄ μ»΄νμΌν λ λ°λ‘ μ μ μλ€.
λ°νμμ μ€μλ₯Ό μμ차리λ κ²λ³΄λ€ μ»΄νμΌ μμ μ μ€μλ₯Ό μμ차리λ κ² λ λμ κ²μ΄λ€.
π 2. λ°°μ΄μ μ€μ²΄ν(reify)λκ³ , μ λ€λ¦μ νμ μ λ³΄κ° μκ±°λλ€.
λ°°μ΄μ μ€μ²΄ν(reify)λλ€. λ°°μ΄μ λ°νμμλ μμ μ΄ λ΄κΈ°λ‘ ν μμμ νμ μ μΈμ§νκ³ νμΈνλ€. κ·Έλ κΈ° λλ¬Έμ Longλ°°μ΄μ Stringμ λ£μΌλ €κ³ νλ©΄ ArrayStoreExceptionμ΄ λ°μνλ€.
public static void main(String[] args) {
Object[] objectArray = new Long[1];
objectArray[0] = "νμ
μ΄ λ¬λΌμ λ£μ μ μλ€."; // ArrayStoreException (λ°νμμ λ°μ)
}
λ°λ©΄μ μ λ€λ¦μ νμ μ λ³΄κ° λ°νμμλ μκ±°(erasure)λλ€. μμ νμ μ μ»΄νμΌνμμλ§ κ²μ¬νλ©΄ λ°νμμλ μ μμ‘°μ°¨ μκ² λλ€.
2. μ λ€λ¦ λ°°μ΄μ μ¬μ©ν μ μλ€. π
μμμ μ΄ν΄λ³Έ λ°°μ΄κ³Ό μ λ€λ¦μ μ°¨μ΄μ μΌλ‘ μΈν΄μ λ°°μ΄κ³Ό μ λ€λ¦μ μ μ΄μ°λ¬μ§μ§ λͺ»νλ€. κ·Έλμ λμ μμ΄ μ°κΈ°κ° μ½μ§ μλ€.
λ°°μ΄μ μ λ€λ¦ νμ , 맀κ°λ³μν νμ , νμ 맀κ°λ³μλ‘ μ¬μ©ν μ μλ€. μ¦, μ½λλ₯Ό new List<E>[], new List<String>[], new E[] μμΌλ‘ μμ±νλ©΄ μ»΄νμΌν λ μ λ€λ¦ λ°°μ΄ μμ± μ€λ₯κ° λ°μνλ€.
μ λ€λ¦ λ°°μ΄μ λ§λ€μ§ λͺ»νκ² λ§μ μ΄μ λ νμ μ΄ μμ νμ§ μκΈ° λλ¬Έμ΄λ€.
μ λ€λ¦ λ°°μ΄μ λ§λ€λλ‘ νμ©νκ² λλ©΄ μ»΄νμΌλ¬κ° μλ μμ±ν νλ³ν μ½λμμ λ°νμμ ClassCastExceptionμ΄ λ°μν μ μλ€. μ΄κ²μ λ°νμμ ClassCastExceptionμ΄ λ°μνλ μΌμ λ§μμ£Όκ² λ€λ μ λ€λ¦ νμ μ μ·¨μ§μ μ΄κΈλλ κ²μ΄λ€.
μ½λλ₯Ό ν΅ν΄ μ λ€λ¦ λ°°μ΄μ νμ©νλ©΄ μ΄λ»κ² λλμ§ μ΄ν΄λ³΄μ.
List<String>[] stringLists = new List<String>[1]; // (1)
List<Integer> intList = List.of(42); // (2)
Object[] objects = stringLists; // (3)
objects[0] = intList; // (4)
String s = stringLists[0].get(0); // (5)
- (1)μ΄ νμ©λλ€κ³ κ°μ ν΄ λ³΄μ.
- (2)λ μμκ° νλμΈ List<Integer>λ₯Ό μμ±νλ€.
- (3)μ (1)μμ μμ±ν List<String>μ λ°°μ΄μ Object λ°°μ΄μ ν λΉνλ€. λ°°μ΄μ 곡λ³μ΄κΈ° λλ¬Έμ Object λ°°μ΄μ List<String> λ°°μ΄μ ν λΉν μ μλ€.
- (4)λ (2)μμ μμ±ν List<Integer>μ μΈμ€ν΄μ€λ₯Ό Object λ°°μ΄μ 첫 μμλ‘ μ μ₯νλ€. μ λ€λ¦μ μκ±° λ°©μμΌλ‘ ꡬνλκΈ° λλ¬Έμ μ΄κ² λν κ°λ₯νλ€.
- (5)μμ λ¬Έμ κ° λ°μνλ€. (1)~(4)μ κ³Όμ μ κ±°μΉλ©΄μ (1)μμ List<String>λ§ λ΄μΌλ €κ³ νλ λ°°μ΄μ List<Integer>κ° λ΄κ²¨ μλ κ²μ΄λ€. κ·Έλμ (5)μμ μ»΄νμΌλ¬λ κΊΌλΈ μμλ₯Ό μλμΌλ‘ StringμΌλ‘ νλ³ννλλ°, μ΄ μμλ Integerμ΄λ―λ‘ λ°νμμ ClassCastExceptionμ΄ λ°μνλ€.
κ·Έλ κΈ° λλ¬Έμ μ²μλΆν° μ λ€λ¦ λ°°μ΄μ΄ μμ±λμ§ μλλ‘ (1)μμ μ»΄νμΌ μ€λ₯κ° λμΌ νλ€.
3. λ°°μ΄μ 리μ€νΈλ‘ λ체νλ λ°©λ² π
컬λ μ μμ μμ μ€ νλλ₯Ό 무μμλ‘ μ νν΄ λ°ννλ choose λ©μλλ₯Ό μ 곡νλ ν΄λμ€λ₯Ό μ΄ν΄λ³΄μ.
public class Chooser_Array {
private final Object[] choiceList;
public Chooser_Array(Collection choices) {
choiceList = choices.toArray();
}
public Object choose() {
Random rnd = ThreadLocalRandom.current();
return choiceList[rnd.nextInt(choiceList.length)];
}
}
μ΄ ν΄λμ€λ₯Ό μ¬μ©νλ €λ©΄ choose λ©μλλ₯Ό νΈμΆν λλ§λ€ λ°νλ Objectλ₯Ό μνλ νμ μΌλ‘ νλ³νν΄μΌ νλ€.
public static void main(String[] args) {
List<Integer> intList = List.of(1, 2, 3, 4, 5);
Chooser_Array chooser = new Chooser_Array(intList);
for(int i = 0; i < intList.size(); i++){
Number choice = (Number) chooser.choose(); // μ¬λ°λ₯΄κ² νλ³νμ ν κ²½μ°
System.out.println(choice);
}
}
νμ§λ§ μμ κ°μ΄ μ¬λ°λ₯΄κ² νλ³ν ν μλ μμ§λ§, μλμ κ°μ΄ μ¬λ°λ₯΄μ§ μκ² νλ³ν ν μλ μλ€.
public static void main(String[] args) {
List<String> intList = List.of("amenable", "choi");
Chooser_Array chooser = new Chooser_Array(intList);
for(int i = 0; i < intList.size(); i++){
Number choice = (Number) chooser.choose(); // μ¬λ°λ₯΄μ§ μκ² νλ³νμ ν κ²½μ°
System.out.println(choice);
}
}
μ΄λ° μν©μ μλ°©νκΈ° μν΄μ μ λ€λ¦μ μ¬μ©ν μ μλ€.
public class Chooser_Array<T> {
private final Object[] choiceList;
@SuppressWarnings("unchecked")
public Chooser_Array(Collection<T> choices) {
choiceList = (T[]) choices.toArray(); // Unchecked cast: 'java.lang.Object[]' to 'T[]'
}
@SuppressWarnings("unchecked")
public Object choose() {
Random rnd = ThreadLocalRandom.current();
return choiceList[rnd.nextInt(choiceList.length)];
}
}
public static void main(String[] args) {
List<Integer> intList = List.of(1, 2, 3, 4, 5);
Chooser_Array chooser = new Chooser_Array(intList);
for(int i = 0; i < intList.size(); i++){
Number choice = (Number) chooser.choose();
System.out.println(choice);
}
}
'choiceList = (T[]) choices.toArray();'μμ νμ μμ μ±μ μ»΄νμΌλ¬κ° 보μ₯νμ§ λͺ»νκΈ° λλ¬Έμ κ²½κ³ κ° λ¬λ€. λ―λ‘ μ½λμ μλ κ²μ²λΌ @SuppressWarningsλ₯Ό μ΄μ©ν΄μ κ²½κ³ λ₯Ό μμ¨ μ μλ€.
νμ§λ§, μμ κ²½κ³ κ° λ¨μ§ μλλ‘ νλ λ μ’μ λ°©λ²μ λ°°μ΄μ΄ μλ 리μ€νΈλ₯Ό μ¬μ©νλ κ²μ΄λ€.
public class Chooser_Array<T> {
private final List<T> choiceList;
public Chooser_Array(Collection<T> choices) {
choiceList = new ArrayList<>(choices);
}
public T choose() {
Random rnd = ThreadLocalRandom.current();
return choiceList.get(rnd.nextInt(choiceList.size()));
}
}
public static void main(String[] args) {
List<Integer> intList = List.of(1, 2, 3, 4, 5);
Chooser_Array<Integer> chooser = new Chooser_Array<>(intList);
for(int i = 0; i < intList.size(); i++){
Number choice = chooser.choose(); // νλ³νμ ν νμκ° μλ€.
System.out.println(choice);
}
}
μ΄λ κ² νλ©΄ κ²½κ³ μμ΄ μ»΄νμΌλλ€. λν, choose λ©μλλ₯Ό νΈμΆν λ νλ³νμ ν νμλ μμ΄μ§λ€.
κ·Έλμ λ§μ½ λ°°μ΄κ³Ό μ λ€λ¦μ μμ΄ μ°λ€κ° μ»΄νμΌ μ€λ₯λ κ²½κ³ λ₯Ό λ§λλ©΄, κ°μ₯ λ¨Όμ λ°°μ΄μ 리μ€νΈλ‘ λ체νλ λ°©λ²μ μ μ©ν΄ 보λλ‘ νμ.
ν΄λΉ κΈμ λ°±κΈ°μ λμ 'μ΄νν°λΈ μλ° μλ²½ 곡λ΅'μ μ°Έκ³ νμμ΅λλ€.