
| package _1027_A; import java.util.*; // 키보드로 문자열을 4개 입력 받아 ArrayList에 삽입하고 가장 긴 이름을 출력하라. // 가장 긴 이름은 : Ashley public class Main { public static void main(String[] args) { ArrayList<String> s = new ArrayList<String>(); int index = 0, max = -1; for(int i = 0; i < 4; i++) { Scanner sc = new Scanner(System.in); System.out.print("이름을 입력하세요>>"); s.add(new String(sc.nextLine())); if(max < s.get(i).length()) { max = s.get(i).length(); index = i; } } for (String str : s) { System.out.print(str+" "); } System.out.println("\n"+s.get(index)); } } | cs |

| package _1027_B; import java.util.HashSet; import java.util.Iterator; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub HashSet<Person> hSet = new HashSet<Person>(); hSet.add(new Person("Lee", 24)); hSet.add(new Person("Hong", 29)); hSet.add(new Person("Choi", 21)); hSet.add(new Person("Hong", 28)); printCollection(hSet.iterator()); System.out.println(); deleteCollectionByHash(hSet, "Hong"); printCollection(hSet.iterator()); } public static void printCollection(Iterator<Person> itr) { while (itr.hasNext()) itr.next().ShowData(); } /** * 해쉬 값을 이용하여 해당 오브젝트를 제거 O(1) */ public static void deleteCollectionByHash(HashSet<Person> hSet, String name) { hSet.remove(new Person(name)); } /** * 평범한 방식인 순차탐색을 이용하여 해당 오브젝트를 제거 O(n) */ public static void deleteCollectionBySearch(HashSet<Person> hSet, String name) { Iterator<Person> itr = hSet.iterator(); while (itr.hasNext()) { if (itr.next().GetName().equals(name)) { itr.remove(); } } } } | cs |
| package _1027_B; public class Person { private String mName; private int mAge; public Person(String name, int age) { this.mName = name; this.mAge = age; } public Person(String name) { this.mName = name; } public String GetName() { return this.mName; } public void ShowData() { System.out.println(this.mName + " " + this.mAge); } @Override public int hashCode() { return this.mName.hashCode(); } @Override public boolean equals(Object obj) { Person other = (Person) obj; if (this.mName.equals(other.mName)) return true; return false; } } | cs |

| package _1027C; import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { HashMap<Integer, String> map = new HashMap<Integer, String>(); Scanner input = new Scanner(System.in); int keyInput; String valueInput; while(true) { System.out.print("번호와 단어를 입력하세요(99==quit): "); keyInput = input.nextInt(); valueInput = input.nextLine(); if(keyInput == 99) break; map.put(keyInput, valueInput.substring(1, valueInput.length())); } System.out.print("찾을 단어의 번호를 입력하세요: "); keyInput = input.nextInt(); System.out.print(map.get(keyInput) + "입니다."); } } | cs |
'기타 > univ. projects' 카테고리의 다른 글
| [유니티] 게임엔진응용실습 Unit5 프로젝트 - Tower Defense (Windows & Android) (0) | 2022.11.05 |
|---|---|
| [자바] 멀티미디어자바프로젝트II ch.08 연습문제 (0) | 2022.11.01 |
| [자바] 멀티미디어자바프로젝트II ch.06 연습문제 (0) | 2022.10.22 |
| [유니티] 게임엔진응용실습 Unit4 프로젝트 - Airplane Shooting (Space Invader) (0) | 2022.10.22 |
| [자바] 멀티미디어자바프로젝트II ch.05 연습문제 (0) | 2022.10.06 |