Scanner 클래스로 -1이 입력될 때까지
양의 정수를 입력 받아 저장하고
검색하여 가장 큰 수를 출력하는
프로그램을 작성하라.
==================
정수(-1이 입력될 때까지)>> 10 6 22 6 88 77 -1
가장 큰 수는 88
public static void main(String[] args) { List<Integer>list = new LinkedList<>(); Scanner sc = new Scanner(System.in); while(true) { System.out.println("정수(-1이 입력될때 까지)>>"); int num = sc.nextInt(); if (num == -1) break; list.add(num); } int max=0; for (int num : list) { if(num > max) max = num; } System.out.println("가장 큰 수는 : " + max); }
}
Map이란 Key와 Value를 기반으로 만든 순서 없는 자료 구조
Key는 중복 불가 Set으로 구현되어 있다.
3.아래의 TreeMap의 Value를 확인 하기 위한 소스를 짜시오.(필수)
TreeMap<Integer, String> map = new TreeMap<>();
map.put(45, "Brown");
map.put(37, "James");
map.put(23, "Martin");
============<결과값>============
45 Brown
37 James
23 Martin
Treemap 과 Hashmap 의 차이는?
Treemap : binary Tree를 기반으로 map을 구현한 것이고 Hashmap은 Hash를 기반으로 Map을 구현한 것 Treemap는 binary tree가 기반이기 때문에 생성 시 정렬을 하면서 들어가게 된다.
Hashmap : hash algorithm을 통해 검색하기 때문에 검색이 더 빠르다.
Deque 로 Stack 을 구현하시오.
Deque<String> deq = new ArrayDeque<>(); //앞으로 넣고 deq.offerFirst("1. Box"); deq.offerFirst("2. Toy"); deq.offerFirst("3. Robot"); //앞에서 꺼내기 System.out.println(deq.pollFirst()); System.out.println(deq.pollFirst()); System.out.println(deq.pollFirst()); }
}
===================<결과값>=================
3. Robot
2. Toy
1. Box