๐ŸŸฉ ์ œ๋„ค๋ฆญ(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"));

+ Recent posts