collection : sava data /(like interface)
collection core interface > List, Set, Queue, Deque, / Map
걍 List<String> aa= new ArrayList 랑 비슷 개념.(List가 ArrayList, linkedList, vector등으로 맘대로 정의가능한것처럼.)
>collection은 더 포괄적(generic)한 개념
EX)
private Collection<Seat> seats = new Listhashset<>();
private Collection<Seat> seats = new LinkedList<>(); new LinkedHashSet .........collection 하위에 있는것들로 define가능
(단, TreeSet처럼 one level down은 collection으로 directly define불가)
https://docs.oracle.com/javase/tutorial/collections/interfaces/index.html (collection core interface 그림)
https://www.crocus.co.kr/1553 (그림 참고 - 한국어)
하단에 string.format 기본개념 링크
개념예제)
해당 극장에 해당 row에 how many seats ? (ArrayList 이용한 경우와 Collection 이용한 경우 비교.)
1. ///Theater Class , InnerClass -> OutPut 순서대로 출력된다. (ArrayList)
package com.timbuchalka;
import java.util.*; // util.* 로 수정하면 util의 하위packages 다 쓸수 있음.
//극장에 좌석 여러개- (row 별로)
public class Theatre {
private final String theatreName;
private List<Seat> seats = new ArrayList<>(); //seat arrayList define V
//constructor
public Theatre(String theatreName, int numRows, int seatsPerRow) {
this.theatreName = theatreName;
//seats detail define
int lastRow = 'A' + (numRows -1);
for (char row = 'A'; row <= lastRow; row++) { //ex)CGV극장은 한줄에 6좌석.
for(int seatNum = 1; seatNum <= seatsPerRow; seatNum++) { //좌석은 1부터 시작
Seat seat = new Seat(row + String.format("%02d", seatNum));
seats.add(seat); //string.format : 0붙인 2자리 정수
} V
}
}
//getter
public String getTheatreName() {
return theatreName;
}
//method
public boolean reserveSeat(String seatNumber) { //좌석 존재 여부 후 좌석예약(InnerClass)
Seat requestedSeat = null; //define
for(Seat seat : seats) { //예약원하는 좌석이 존재한다면, 동일시..
if(seat.getSeatNumber().equals(seatNumber)) {
requestedSeat = seat;
break;
}
}
if(requestedSeat == null) { //좌석이 존재하지 않으면, 예약 false
System.out.println("There is no seat " + seatNumber);
return false;
}
return requestedSeat.reserved; //
}
// for testing //getter
public void getSeats() {
for(Seat seat : seats) {
System.out.println(seat.getSeatNumber());
}
}
//Seat class따로 안만들고 걍 innerClass로 넣어버림
private class Seat {
private final String seatNumber;
private boolean reserved = false; //default : no reserved
//constructor
public Seat(String seatNumber) {
this.seatNumber = seatNumber;
}
//예약 method
public boolean reserve() {
if(!this.reserved) { //this.reserved == false 라면
this.reserved = true;
System.out.println("Seat " + seatNumber + " reserved");
return true;
} else { //this. reserved == true 라면
return false;
}
}
// cancel method
public boolean cancel() {
if(this.reserved) {
this.reserved = false; //좌석 공석으로
System.out.println("Reservation of seat " + seatNumber + " cancelled");
return true;
} else {
return false;
}
}
//getter
public String getSeatNumber() {
return seatNumber;
}
}
//Main / Output -> 좌석 확인해보자. -> 순서대로 나열된다.
//Main
package com.timbuchalka;
public class Main {
public static void main(String[] args) { //row 8개,한row에 좌석 12개
Theatre theatre = new Theatre("Olympian", 8, 12); //call constructor
theatre.getSeats(); //object.getter
System.out.println("************************");
if(theatre.reserveSeat("H11")) {
System.out.println("Please pay");
} else {
System.out.println("Sorry, seat is taken");
}
if(theatre.reserveSeat("H11")) {
System.out.println("Please pay");
} else {
System.out.println("Sorry, seat is taken");
}
}
}
//Output
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=51586:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\Java-Collections-Collections-Overview-Source-code\out\production\Collections com.timbuchalka.Main
A01 //row : A~H(8개)
A02 // seatsPerRow : 1~12(12개)
A03 // String.format("%02d")
A04
A05
A06
A07
A08
A09
A10
A11
A12
B01
B02
B03
B04
B05
B06
B07
B08
B09
B10
B11
B12
C01
C02
C03
C04
C05
C06
C07
C08
C09
C10
C11
C12
D01
D02
D03
D04
D05
D06
D07
D08
D09
D10
D11
D12
E01
E02
E03
E04
E05
E06
E07
E08
E09
E10
E11
E12
F01
F02
F03
F04
F05
F06
F07
F08
F09
F10
F11
F12
G01
G02
G03
G04
G05
G06
G07
G08
G09
G10
G11
G12
H01
H02
H03
H04
H05
H06
H07
H08
H09
H10
H11
H12
************************
Seat H11 reserved // V
Please pay
Sorry, seat is taken // V
Process finished with exit code 0
2. Collection을 이용해보자. (Collection - LinkedList) (Collection - HashSet)
-01 (Collection - LinkedList) 결과 동일(순서대로 출력)
package com.timbuchalka;
import java.util.*; // util.* 로 수정하면 util의 하위packages 다 쓸수 있음.
//극장에 좌석 여러개- (row 별로)
public class Theatre {
private final String theatreName;
private Collection<Seat> seats = new LinkedList<>(); // V
//constructor
public Theatre(String theatreName, int numRows, int seatsPerRow) {
this.theatreName = theatreName;
//seats detail define
int lastRow = 'A' + (numRows -1);
for (char row = 'A'; row <= lastRow; row++) { //ex)CGV극장은 한줄에 6좌석.
for(int seatNum = 1; seatNum <= seatsPerRow; seatNum++) { //좌석은 1부터 시작
Seat seat = new Seat(row + String.format("%02d", seatNum));
seats.add(seat); //string.format : 0붙인 2자리 정수
} V
}
}
//나머지 모두 동일 coding.
//Output
// 결과값 여전히 동일
// private List<Seat> seats = new ArrayList<>(); 일때와 동일.
-02 (Collection - HashSet) > 결과다름 : 순서 무작위로 출력
package com.timbuchalka;
import java.util.*; // util.* 로 수정하면 util의 하위packages 다 쓸수 있음.
//극장에 좌석 여러개- (row 별로)
public class Theatre {
private final String theatreName;
private Collection<Seat> seats = new HashSet<>(); // V
//constructor
public Theatre(String theatreName, int numRows, int seatsPerRow) {
this.theatreName = theatreName;
//seats detail define
int lastRow = 'A' + (numRows -1);
for (char row = 'A'; row <= lastRow; row++) { //ex)CGV극장은 한줄에 6좌석.
for(int seatNum = 1; seatNum <= seatsPerRow; seatNum++) { //좌석은 1부터 시작
Seat seat = new Seat(row + String.format("%02d", seatNum));
seats.add(seat); //string.format : 0붙인 2자리 정수
} V
}
}
//나머지 모두 동일 coding.
//Output
// 결과값 다름 - 순서 무작위로 출력
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=52258:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\Java-Collections-Collections-Overview-Source-code\out\production\Collections com.timbuchalka.Main
B04
H09
D05
H11
B10
F09
D07
E08
G11
E03
E11
E01
F04
B02
A09
C04
C06
B05
F11
G02
H07
G05
C09
B08
B11
A11
C12
A06
E07
C08
F02
F10
A07
H05
A04
E10
F12
C03
F03
D12
G07
G10
G08
D03
D11
A05
E04
G04
C02
H03
B06
B07
D01
H06
G09
B01
A12
A03
C01
A02
B09
E02
C10
F07
C07
H08
A08
G01
A01
H12
D09
F01
G12
C05
D02
F05
D04
E05
E09
H01
E12
H04
E06
D08
F08
D10
G03
C11
G06
A10
B12
H02
B03
H10
D06
F06
************************
Seat H11 reserved
Please pay
Sorry, seat is taken
Process finished with exit code 0
<String.format>
https://a1010100z.tistory.com/63
[Java] String.format 에 대해 알아보자(백준 알고리즘 2439)
알고리즘 문제를 풀다보면 자릿수를 맞춰야 하는 문제가 하나씩 보이는데, 예를들면, 0010을 출력해야 하는데 int형으로 출력하면 00이 생략된다던가 모든 스트링을 오른쪽 정렬해야한다던가. 이
a1010100z.tistory.com
https://library1008.tistory.com/5
문자열 포맷 - String.format(), System.out.printf()
C 언어에서 출력을 담당하는 printf() 함수를 사용해 보신적이 있으신가요? 자바(Java)에서도 동일한 기능을 제공하는 메소드들이 존재합니다. String 클래스의 format() 메소드 : String.format() PrintWriter ��
library1008.tistory.com
'JavaCode(review)' 카테고리의 다른 글
Comparable / Comparator - Theater 코딩 이용 (0) | 2020.07.04 |
---|---|
Collections.binarySearch(); / Theater코딩에 binarySearch 이용 (0) | 2020.07.03 |
자바 용어 사이트 oracle java tutorial (0) | 2020.07.02 |
binary search tree (0) | 2020.07.01 |
Static initialization block (0) | 2020.06.30 |