반응형
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
- paragraph
- 웹 용어
- vscode
- web 용어
- 머신러닝
- cudnn
- CES 2O21 참여
- broscoding
- 재귀함수
- 자료구조
- web 개발
- 데이터전문기관
- C언어
- pycharm
- bccard
- 결합전문기관
- Keras
- html
- inorder
- web 사진
- mglearn
- classification
- discrete_scatter
- postorder
- tensorflow
- CES 2O21 참가
- web
- 대이터
- KNeighborsClassifier
- java역사
Archives
- Today
- Total
bro's coding
java.iterator(열거형) 본문
반응형
Iterator
반복자, java.util.Collection의 abstract method
iterator() 메서드의 리턴타입
hasNext() : boolean
> 다음 요소로 이동해 존재유무를 확인 후 true/false
next() : Element
> 다음 요소를 추출
package step7;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
public class TestIterator {
public static void main(String[] args) {
LinkedHashSet<String> set = new LinkedHashSet<String>();
set.add("삼겹살");
set.add("참이슬");
set.add("카스");
Iterator<String> it = set.iterator();
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
//
/*
* 삼겹살 참이슬 카스
*/
while (it.hasNext()) {// 다음 요소가 있는지 확인(있다:true/없다:false)
System.out.println(it.next());
}
/*
* 삼겹살 참이슬 카스
*/
ArrayList<String> list = new ArrayList<String>();
list.add("피자");
list.add("맥주");
list.add("황태");
Iterator<String> it2=list.iterator();
while(it2.hasNext()) {
System.out.println(it2.next());
}
/*
피자
맥주
황태
*/
}
}
package step8;
import java.util.Collection;
import java.util.Iterator;
public class TestIterator2 {
public static void main(String[] args) {
DataService service = new DataService();
int type = 1;
type = 2;
// 반환되는 자료구조체가 Set 계열이든 List 계열이든 관계없이 단일한 방식으로 요소들을 열거할 수 있다.
Collection<String> dataSet = service.getDataSet(type);
System.out.println(dataSet);
Iterator<String> it = dataSet.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
package step8;
import java.util.ArrayList;
import java.util.Collection;
import java.util.TreeSet;
public class DataService {
// 매개변수로 전달되는 type 정보가
// 1이면 TreeSet<String>객체가 반환
// 2이면 ArrayList<String>객체가 반환
// 리턴타입은 상위 인터페이스 타입인 Collection이 적합
public Collection<String> getDataSet(int type) {
// TODO Auto-generated method stub
Collection<String> dataSet = null;
if (type == 1) {
dataSet = new TreeSet<String>();
dataSet.add("맥주");
dataSet.add("골뱅이");
}
else if(type==2) {
dataSet=new ArrayList<String>();
dataSet.add("삼겹살");
dataSet.add("소주");
dataSet.add("광어");
}
return dataSet;
}
}
javajava.Collection.List
List 계열 : 순수를 보장(인덱스로 관리) ArrayList(검색에 용이) LinkedList(추가, 수정, 삭제에 용이) package step5; import java.util.ArrayList; public class TestList1 { public static void main(String[..
broscoding.tistory.com
java.Interface
interface 약속 or 계약 구현한 측과 사용하는 측과의 약속 -> 표준화 > interface는 다중 상속의 장점을 채택 > 다중 상속의 장점은 다양한 계층 구조 형성을 통한 다형성 적용에 있음 > 즉 interface는 다
broscoding.tistory.com
반응형
'[IT] > java' 카테고리의 다른 글
java.map (0) | 2021.03.09 |
---|---|
java.stack/queue (0) | 2021.03.09 |
java.ArrayList/LinkedList (0) | 2021.03.09 |
java.SemiProject.SchoolService (0) | 2021.03.08 |
java.DTO/DAO/VO/Service (0) | 2021.03.05 |
javajava.Collection.List (0) | 2021.03.05 |
java.Random (0) | 2021.03.05 |
java.Generic (0) | 2021.03.05 |
Comments