코딩/2-JAVA

C25제네릭

tree0505 2025. 7. 22. 16:11
반응형
    • C2501제네릭_개념

  • 제네릭_기본이론1_정수
package C2501제네릭_개념;


import java.util.Scanner;

class MyVector1 {

	private int[] arr;
	private int count;

	public void print() {
		System.out.print("[");
		for (int i = 0; i < count; i++) {
			System.out.print(arr[i]);
			if (i != count - 1) {
				System.out.print(", ");
			}
		}
		System.out.println("]");
	}

	public void add(int value) {
		if (count == 0) {
			arr = new int[count + 1];
		} else if (count > 0) {
			int[] temp = arr;
			arr = new int[count + 1];

			for (int i = 0; i < count; i++) {
				arr[i] = temp[i];
			}
			temp = null;
		}

		arr[count] = value;
		count += 1;
	}

	public void add(int index, int value) {
		if (count == 0) {
			arr = new int[count + 1];
		} else if (count > 0) {
			int[] temp = arr;
			arr = new int[count + 1];

			int j = 0;
			for (int i = 0; i < count + 1; i++) {
				if (i != index) {
					arr[i] = temp[j];
					j += 1;
				}
			}
			temp = null;
		}

		arr[index] = value;
		count += 1;
	}

	public void remove(int index) {
		if (count == 1) {
			arr = null;
		} else if (count > 1) {
			int[] temp = arr;
			arr = new int[count - 1];

			int j = 0;
			for (int i = 0; i < count; i++) {
				if (i != index) {
					arr[j] = temp[i];
					j += 1;
				}
			}
			temp = null;
		}
		count -= 1;
	}

	public int size() {
		return count;
	}

	public int get(int index) {
		return arr[index];
	}

	public void set(int index, int value) {
		arr[index] = value;
	}

	public void clear() {
		arr = null;
		count = 0;
	}
	
}

public class 제네릭_기본이론1_정수 {
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);

		MyVector1 list =new MyVector1();
		
		while(true) {
			list.print();
			
			System.out.println("1.추가 2.삭제 3.삽입 4.종료");
			int sel = scan.nextInt();
			if(sel == 1) {
				System.out.println("추가할 숫자를입력하세요 >> ");
				int num = scan.nextInt();
				list.add(num);
			}else if(sel == 2) {
				list.print();
				System.out.println("삭제할 인덱스를 입력하세요 >> ");
				int index = scan.nextInt();
				if(list.size() <= 0) {
					continue;
				}
				if(index < 0) {
					continue;
				}
				if(index >= list.size()) {
					continue;
				}
				list.remove(index);
				
			}else if(sel == 3) {
				list.print();
				System.out.println("삽입할 위치를 입력하세요 >> ");
				int pos = scan.nextInt();
				System.out.println("삽입할 값을 입력하세요 >> ");
				int value = scan.nextInt();
				list.add(pos , value);
				
			}else if(sel == 4) {
				System.out.println("종료");
				break;
			}
		}
		scan.close();
	}
}

  • 제네릭_기본이론2_클래스
package C2501제네릭_개념;

class MyVector2 {
	
	private Tv[] arr;
	private int count;

	public void add(Tv e) {
		if (count == 0) {
			arr = new Tv[count + 1];
		} else if (count > 0) {
			Tv[] temp = arr;

			arr = new Tv[count + 1];
			for (int i = 0; i < count; i++) {
				arr[i] = temp[i];
			}
		}
		arr[count] = e;
		count += 1;
	}

	public int size() {
		return count;
	}

	public void remove(int index) {
		if (count == 1) {
			arr = null;
			count = 0;
		} else if (count > 1) {
			Tv[] temp = arr;

			arr = new Tv[count - 1];
			int j = 0;
			for (int i = 0; i < count; i++) {
				if (i != index) {
					arr[j] = temp[i];
					j += 1;
				}
			}
		}
		count -= 1;
	}

	public Tv get(int index) {
		return arr[index];
	}

}
class Tv {
	public String name;
	public String brand;
	public int price;

	public Tv(String name, String brand, int price) {
		this.name = name;
		this.brand = brand;
		this.price = price;
	}
}


public class 제네릭_기본이론2_클래스 {
	public static void main(String[] args) {
		
		MyVector2 myList = new MyVector2();
		
		Tv temp = new Tv("TV", "삼성", 1000);
		myList.add(temp);

		temp = new Tv("시그니처TV", "엘지", 2000);
		myList.add(temp);

		temp = new Tv("스마트TV", "애플", 3000);
		myList.add(temp);

		temp = myList.get(1);
		System.out.println(temp.name);

	}
}

  • 제네릭_기본이론3_제네릭_암기
package C2501제네릭_개념;

import java.util.Vector;

class MyVector<T> { //제네릭은 <T>이다. 
	
	private Object[] arr;
	private int count;

	public void add(T data) {
		if (count == 0) {
			arr = new Object[1];
		} else if (count > 0) {
			Object[] temp = arr;

			arr = new Object[count + 1];
			for (int i = 0; i < count; i++) {
				arr[i] = temp[i];
			}

			temp = null;
		}

		arr[count] = data;
		count++;
	}

	public void set(int index, T data) {
		arr[index] = data;
	}

	public int size() {
		return count;
	}

	public T get(int index) {	
		return (T)arr[index];			
	}
}

class User {
	public String name;

	public User() {}

	public User(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return name;
	}
}

public class 제네릭_기본이론3_제네릭_암기 {
	public static void main(String[] args) {
		/* 
		 * <제네릭> <T> t는 템플릿의 약자로 다른문자를 사용해도 무방하다. 예) <K> 
		 *  제네릭은 <> 를 뜻하고 클래스가 정해지지않고 대기하다가 
		 *  <> 안에 타입을 명시해주면 그때 클래스가 정해지는걸 말한다. 
		 *  
		 *  지금까지 사용했던 Vector<클래스타입>  가 제네릭의 대표적 사용법이다.  
		 *  딱히, Vector 이외의 다른용도로는 사용하지않는다. 
		 * -------------------------------------------------------
		 *  실제 Vector 는 제네릭을 사용하여 아래와똑같은 방법으로 만들어졌다. 
		 */
		MyVector<User> list = new MyVector<User>();
		
		list.add(new User("홍길동"));
		list.add(new User("김민수"));
		
		User temp = new User("김소정");
		list.add(temp);
		
		list.set(1, new User("최민정"));
		
		for(int i=0; i<list.size(); i++) {
			System.out.println(list.get(i).toString());
		}
		
		// <> 제네릭은 클래스를 지정해주는대로 사용가능하다. 
		MyVector<Tv> list2 = new MyVector<Tv>();	
		MyVector<Integer> list3 = new MyVector<Integer>();
		
		
		System.out.println("-------------------------------");
		Vector<User> vecList= new Vector<>();
		vecList.add(new User("홍길동"));
		vecList.add(new User("김민수"));
		
		User temp1 = new User("김소정");
		vecList.add(temp1);
		
		vecList.set(1, new User("최민정"));
		
		for(int i=0; i<vecList.size(); i++) {
			System.out.println(vecList.get(i).toString());
		}
		
	}

}

  • 제네릭 <T> => T는 템플릿의 약자 
  • Vector <String> a = new Vector <> () ; 
  • MyVector <T> {}
  • MyVector <String> 
  • MyVector<Integer>
반응형

'코딩 > 2-JAVA' 카테고리의 다른 글

C23날짜  (0) 2025.07.22
C21콜렉션응용  (4) 2025.07.18
C20인터페이스  (1) 2025.07.18
C19추상화  (1) 2025.07.17
C18상속  (2) 2025.07.17