- ArrayList에 0~100사이의 임의의정수 10개를 삽입하고 모두출력하시오.
- 출력할때는 Iterator인터페이스를 사용해서 출력하시오.
import java.util.ArrayList;
import java.util.Iterator;
public static void main(String[] args) {
ArrayList<Integer> a =
new ArrayList<Integer>();
for(int i=0; i<10; i++) {
int num = (int) (Math.random()*101);
a.add(num);
}
Iterator<Integer> it = a.iterator();
while(it.hasNext()) {
System.out.print( it.next()+" " );
}
}//main end
- Map을 만들어 이름, 나이를 저장하고, 이름과 일치하는 나이를 출력하시오
public static void main(String[] args) {
HashMap<String, Integer> h =
new HashMap<String, Integer>();
h.put("세종", 25);
h.put("정조", 34);
h.put("세조", 43);
h.put("문종", 52);
h.put("태조", 61);
Set<String> keys = h.keySet();
Iterator<String> it = keys.iterator();
while(it.hasNext()) {
String name = it.next();
Scanner sc = new Scanner(System.in);
System.out.println("검색할 이름:");
String schName = sc.next();
int reAge = h.get(name);
System.out.printf("%s의 나이는 %d이다.\n",schName, reAge);
}
}//main end
728x90
728x90
'문제풀이' 카테고리의 다른 글
GUI - HashMap을 이용한 단어 입력, 검색 구현 (0) | 2022.06.08 |
---|---|
이름에 해당하는 id 맞추기 게임 (0) | 2022.06.08 |
Review 0606 (0) | 2022.06.06 |
객체배열과 ArrayList (0) | 2022.05.30 |
Review 0527 (0) | 2022.05.30 |