부트캠프/숙제
[Java 문법 종합반] 2주차 - 숙제
purple95
2024. 7. 23. 16:45
2주차강의 과제, 많은 자료구조 내용을 한방에 담아서 강의하고, 과제 역시도 여러 가지를 한방에 사용해보는
역시나 4개월 9to9 다운 과제구나 생각이 들었다, 작성한 코드를 훑어봐도 되게 빠른 속도로 성장을 요구하고, 해야하는구나 생각하게됩니다.
import java.util.*;
public class W01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<String> recipeList = new ArrayList<>();
//저장 순서를 유지하여 출력해야하므로 Set은 LinkedHashSet을 사용해야만함
Set<String> recipeSet = new LinkedHashSet<>();
Map<Integer, String> recipeMap = new HashMap<>();
System.out.print("저장할 자료구조 명 입력 : ");
String dataStructure = sc.nextLine();
System.out.print("내가 좋아하는 요리 제목 : ");
String cookName = sc.nextLine();
//hashMap에서 사용할 idx
int number = 1;
//사용할 DS 구조 입력
switch (dataStructure) {
case "List":
while (true) {
String recipeLine = sc.nextLine();
if (recipeLine.equals("끝"))
break;
recipeList.add(recipeLine);
}
//출력부분
System.out.println("[" + dataStructure + " 으로 저장된 " + cookName + " ]");
for (String line : recipeList) {
System.out.println(number + ". " + line);
number++;
}
break;
case "Set":
while (true) {
String recipeLine = sc.nextLine();
if (recipeLine.equals("끝"))
break;
recipeSet.add(recipeLine);
}
//출력부분
System.out.println("[" + dataStructure + " 으로 저장된 " + cookName + " ]");
for (String line : recipeSet) {
System.out.println(number + ". " + line);
number++;
}
break;
case "Map":
int idx = 1;
while (true) {
String recipeLine = sc.nextLine();
if (recipeLine.equals("끝"))
break;
recipeMap.put(idx++, recipeLine);
}
//출력부분
System.out.println("[" + dataStructure + " 으로 저장된 " + cookName + " ]");
for (int i = 0; i < recipeMap.size(); i++) {
System.out.println(number + ". " + recipeMap.get(number++));
}
break;
}
}
}
향상된 for문을 사용하여, Iterator 없이 요소들을 꺼내올 수 있었다.
팀원의 코드를 같이 공유하고 봤는데 Iterator를 사용하여 .next()를 사용하면서 요소들을 차례대로 가져오는 코드를
보았었다. 이 부분은 jsp&servlet 개인 프로젝트에서 도서 목록을 DB에서 가져와 화면에 출력할 때 사용한적이 있다.
추후 Spring boot를 사용하여 프로젝트를 진해 할 때 다시 한 번 사용하면서 상기시킬 기회가 있을것이라고 확신한다.
작성하면서 Map에 대해서 무지했다는 것을 느꼈다, 실제로 코딩테스트 문제들을 풀면서 List, Set은 자주 사용하지만
Map 같은 경우는 아직 레벨이 낮아서 그런지 사용빈도가 확실히 적기때문,
이 부분에 대해서는 추후 TIL로 정리하는날이 올 것이다.