Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Q 클래스
- Error creating bean with name
- Spring Spring boot 차이
- json gson 차이
- JoinColumn
- 1차캐시
- jpa에러
- json
- Unsatisfied dependency
- ERD 작성
- 스프링 부트 기능
- REST란
- queryDSL
- @IdClass
- jpa회원가입
- REST API 규칙
- uncheck Exception
- git
- github
- 복합키
- JPA
- 스프링 부트 공식 문서
- JPA주의사항
- 빈생성안됨
- 인텔리제이
- Filter
- 최종 프로젝트
- spring서버
- 스프링부트오류
- jwt메서드
Archives
- Today
- Total
Everyday Dev System
0523_TIL : Iterator 필요성 본문
# 문제점 : set과 List를 같이 사용할 경우 각자 다른 코드를 입력하여 코드가 좋지 못했다.
switch (dataStructure){
case "Set" :
Set<String> cookRecipeSet = new HashSet<>();
for(int i=0;i<10;i++) {
cookRecipeSet.add(sc.nextLine());
}
System.out.println("\n[ " + dataStructure + " 으로 저장된 " + cookName);
int num=1;
for(String value : cookRecipeSet){
System.out.println(num + ". " + value);
num++;
}
cookRecipeSet.clear();
break;
case "List" :
ArrayList<String> cookRecipeList = new ArrayList<String>();
for(int i=0;i<10;i++) {
cookRecipeList.add(sc.nextLine());
}
System.out.println("\n[ " + dataStructure + " 으로 저장된 " + cookName);
num=1;
for(String value : cookRecipeList){
System.out.println(num + ". " + value);
num++;
}
cookRecipeList.clear();
break;
case "Map" :
Map<Integer, String> cookRecipeMap = new HashMap<>();
for(int i=1;i<11;i++) {
cookRecipeMap.put(i,sc.nextLine());
}
System.out.println("\n[ " + dataStructure + " 으로 저장된 " + cookName);
num = 1;
for(String value : cookRecipeMap.values()){
System.out.println(num + ". " + value);
num++;
}
cookRecipeMap.clear();
break;
default:
System.out.println("잘못된 자료 구조를 입력하셨습니다.");
break;
}
# 해결방법 : 유지보수성을 향상 시키기 위해 Iterator를 사용
- 조언해주신 분 : 상의성 스승님
Set 타입에는 get 메서드가 없어서 Set과 List를 출력할 때 각각 코드를 따로 입력했다.
하지만 아래와 같이 Iterator을 사용하면 한번만 입력해도 출력이 가능하다.
Iterator<String> iterSet = stringSet.iterator();
int cnt = 0;
while(iterSet.hasNext()) {
++cnt;
System.out.println(cnt + ". " + iterSet.next());
}
'내배캠 주요 학습 > TIL : Today I Learned' 카테고리의 다른 글
0524_TIL : git pull이 안될 경우 (0) | 2023.05.24 |
---|---|
0524_TIL : 인터페이스 자동 형 변환 (0) | 2023.05.24 |
0523_TIL : Java 3주차 강의에서 새로 알게된 것 (0) | 2023.05.23 |
0523_TIL : Java 2주차 강의에서 새로 알게된 것 (0) | 2023.05.23 |
0519_TIL : 파이썬 MongoDB 연동 (0) | 2023.05.23 |