๐ฉ ์ ๋ค๋ฆญ(Generic)์ด๋?
์ฝ๋ ์ ์ ์ฌ์ฉํ๋ฉด์ ๋ฐํํ๋ ๊ฐ์ด ๋ฌด์ ์ธ์ง ๋ชจ๋ฅด๋ฉด ์น๋ช ์ ์ด๊ฒ ๋๋ค.
๊ทธ๋์ ์ฐ๋ฆฌ๋ ์๋ฃํ์์ ๋น์ฐ๊ฒ ๋๋ค.
์๋ฐ์์๋ ์์ฑ๋ ์๋ฃํ์์ผ๋ก Object๋ฅผ ๋ง๋ค๊ณ , ๋ด๊ณ ๋นผ์ค๋ ์ฌ์ฉ์๊ฐ ์ํ๋ ํ์์ผ๋ก ํ๋ณํ์ ํด์ค๋ค.
Object๊ฐ ๋ค์ด์ค๊ณ , ๋๊ฐ๋ ์ฌ์ฉ์๊ฐ ์ํ๋ ์ด๋ ํ ํ์์ผ๋ก ๋ฐ๊ฟ์ฃผ๋ ๊ฒ์ Generic์ด๋ผ๊ณ ํ๋ค.
GList<String,Integer> list = new GList<String,Integer>();
๋ฐ๋์ Object๋ก ๋ณํ์ด ๊ฐ๋ฅํ Wrapperํ์์ผ๋ก ์ ์ด์ค์ผํ๋ค. [int (x) / Integer (o)]
public class GList<ํ์
, ๋๋ฒ์งธํ์
>
public class GList<T>//์์ง์ ์ผ๋ก <T>๋ฅผ ๋ง์ด ์
์์ ์ฝ๋
package com.newlecture.app.util;
public class Program {
public static void main(String[] args) {
GList<Integer> list = new GList<>();
list.add(3);
list.add(5);
int size = list.size();
System.out.printf("size : %d\n",size);
list.clear();
size = list.size();
System.out.printf("size : %d\n",size);
list.add(7);
int num = (Integer) list.get(0); //Object -> Integer
System.out.printf("num : %d\n",num);
//num= (Integer)list.get(1);
}
}
package com.newlecture.app.util;
public class GList<T> {
private Object[] nums;
private int current;
public GList(){
nums = new Object[3];
current =0;
}
public void add(T num) {
nums[current]=num;
current++;
}
public void clear() {
// for(int i=0;i<current;i++)
// nums[i]=0;
//nums = new int[3];
current = 0;
}
public int size() {
return current;
}
public T get(int index) {
if(current<=index)
throw new IndexOutOfBoundsException();
return (T)nums[index];
}
}
๐ฉ ๊ฐ๋ณํฌ๊ธฐ ์ปฌ๋ ์ ์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ
package com.newlecture.app.util;
public class Program {
public static void main(String[] args) {
GList<Integer> list = new GList<>();
list.add(3);
list.add(5);
int size = list.size();
System.out.printf("size : %d\n",size);
list.clear();
size = list.size();
System.out.printf("size : %d\n",size);
list.add(7);
int num = (Integer) list.get(0); //Object -> Integer
System.out.printf("num : %d\n",num);
//num= (Integer)list.get(1);
}
}
package com.newlecture.app.util;
public class GList<T> {
private Object[] nums;
private int current;
private int capacity;//์ฉ๋
private int amount;//์ถ๊ฐ ์ฆ๊ฐ๋
public GList() {
current = 0;
capacity = 3;
amount = 5;
nums = new Object[capacity];
}
public void add(T num) {
//๋ง์ฝ์ ๊ณต๊ฐ์ด ๋ชจ์๋ผ๋ฉด
if (capacity <= current) {
Object[] temp = new Object[capacity + current];
for(int i =0;i<current;i++)
temp[i]=nums[i];
nums = temp;
capacity+=amount;
}
nums[current] = num;
current++;
}
public void clear() {
// for(int i=0;i<current;i++)
// nums[i]=0;
//nums = new int[3];
current = 0;
}
public int size() {
return current;
}
public T get(int index) {
if (current <= index)
throw new IndexOutOfBoundsException();
return (T) nums[index];
}
}
๐ฉ ์๋ฐ ์ปฌ๋ ์ ์ 3๋ ๊ณ๋ณด (Set/List/Map)
List<Integer> list = new ArrayList<>();
list.add(3);//Index 0
list.add(5);//Index 1
list.add(7);//Index 2
list.add(8);//Index 3
list.get(2);// 7์ด๋ผ๋ ๊ฐ์ด ๋์จ๋ค.
System.out.println(list.get(2));
System.out.println(list.size());
// ----------------------------------------------
//์ค๋ณต์ ๊ฑฐํ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉํ๋ค.
Set<Integer> set = new HashSet<>();
set.add(3);
set.add(5);
set.add(6);
set.add(7);
set.add(7);
set.add(7);
System.out.println(set.add(7));
System.out.println(set.add(8));
System.out.println(set.size());
// ---------------------------------------------
Map<String, Object> map = new HashMap<>();
map.put("id", 3);
map.put("title", "Hello");
map.put("hit", 12);
System.out.println(map.get("title"));
'IT > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์์ ๋ด์ฉ ๋ฉ๋ชจ (0) | 2023.08.30 |
---|---|
[JAVA] โ๏ธ Boxing๊ณผ Unboxing (1) | 2023.07.09 |
[Java] ์ปฌ๋ ์ (Collection) ? (1) | 2023.07.09 |
[Java] ํจ์ ์ค๋ฒ๋ก๋(overload) (1) | 2023.07.04 |
[Java]๊ตฌ์กฐ์ ์ธ ํ๋ก๊ทธ๋๋ฐ(๋ฉ์๋) (2) | 2023.06.28 |