[์ ๋ ฌ] 7. ์ขํ ์ ๋ ฌ
<<ํ์ด>>
์ด ๋ฌธ์ ๋ฅผ ์ ๊ทผํ ๋, ํด๋์ค๋ฅผ ์๊ฐ์ ํ์๋ค. ๊ทธ๋ ๊ฒ ํด์ ๋ฐฐ์ด์ด๋ ํด๋์ค์ ๋ฃ์ด์ ์ ๋ ฌ์ ํ๋ ค๊ณ ํ๋๋ฐ, ์ผ๋ฐ์ ์ธ ๋ฐฐ์ด์์๋ ์ฆ, ๊ธฐ๋ณธ ํ์ ์ผ๋๋ Arrays.sort๋ฅผ ํ์ฉํด์ ์ ๋ ฌ์ ํ๋ค๋ฉด ๊ฐ์ฒด ํํ๋ ์ด๋ป๊ฒ ํ ์ง ์๋ฌธ์ด ๋ค์๋ค. ๋น๋ก ํ๋๋ผ๋ ์์ฒญ ๋ณต์กํ ๊ฒ ๊ฐ์๋ค.
๊ฐ์ฌ๋์ ํ์ด๋ฅผ ๋ณด๊ณ ์ด๋ฐ ๋ฐฉ๋ฒ๋ ์๊ตฌ๋ ํ๋ค ใ ใ
-๊ฐ์ฌ๋ ํ์ด-
import java.util.*;
class Point implements Comparable<Point> {
public int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Point o) {
if(this.x == o.x) return this.y - o.y;
else return this.x - o.x;
}
}
class Main{
public static void main(String[] args) {
Main T = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
ArrayList<Point> arr = new ArrayList<>();
for(int i = 0; i < n; i++) {
int x = in.nextInt();
int y = in.nextInt();
arr.add(new Point(x,y));
}
Collections.sort(arr);
for(Point x : arr) System.out.println(x.x + " " + x.y);
}
}
Comparable<T> ๋ผ๋ ์ธํฐํ์ด์ค๋ฅผ ํ์ฉํ๋ ๊ฒ์ด์๋ค. ์ด๊ฒ์ ์ด์ฉํด์ ์ถ์๋ฉ์๋์ธ comepareTo๋ฅผ ์ฌ์ ์ ํ์ฌ ๊ตฌํ๋ ๊ฒ์ด๋ค.
์ฌ์ ์ ํ ๋ ์ค๋ฆ์ฐจ์ ํํ๋ฅผ ์ํ๋ค๋ฉด ์ผ์ชฝ์๋ ํ์ฌ ํด๋์ค ๊ฐ์ ๋ฃ๊ณ ์ค๋ฅธ ์ชฝ์๋ ๋น๊ต ๋์์ ๋ฃ๋ ๊ฒ์ด๋ค. ๋ฐ๋๋ก ๋ด๋ฆผ ์ฐจ์์ ํ ๋์๋ ์ผ์ชฝ์ ๋น๊ต๋์์ ์ค๋ฅธ์ชฝ์ ํ์ฌ ํด๋์ค์ ๊ฐ์ ๋ฃ๋๋ค. ์ด๋ ๊ฒ ํ๊ณ Collection.sort์ ์ด ๊ฐ์ฒด๋ค์ ๋ด์ ArrayList๋ฅผ ๋ฃ์ผ๋ฉด list๊ฐ ์ ๋ ฌ์ด ๋๋ค. ์ด๋ฌํ ๊ฐ๋ ์ด ์๋ค๋ ๊ฒ์ ์ค๋ ์ฒ์ ์์๊ณ ์์ง ์ ๋๋ก ๋ชจ๋ฅด๊ฒ ๋ค ใ ใ ๊ทธ๋์ ๋ฐ๋ก ๊ณต๋ถ๋ฅผ ํด์ผํ ๊ฒ ๊ฐ๋ค
<<์ถ๊ฐ ๊ณต๋ถ>>
Comparable ์ธํฐํ์ด์ค๋?
comparable ์ธํฐํ์ด์ค๋ ๊ฐ์ฒด๊ฐ์ ๋น๊ต๋ฅผ ๊ฐ๋ฅํ๊ฒ ํด์ฃผ๋ ์ธํฐํ์ด์ค์ด๋ค. ํด๋น ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํ(implements)ํ ํด๋์ค๋ ๋ฐ๋์ compareTo<T o> ๋ฉ์๋๋ฅผ ์ฌ์ ์ํด์ผ ํ๋ค.
@Override
public int compareTo(Point o) {
if(this.x == o.x) return this.y - o.y;
else return this.x - o.x;
}
*๊ธฐ๋ณธ์ ์ผ๋ก wrapper ํด๋์ค๋ ์ด๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
์ด ๋ฉ์๋์ ๋ฆฌํด๊ฐ์ ๊ฐ์ง๊ณ Collection.sort()๋ฅผ ํตํด list ์์ ์๋ ๊ฐ์ฒด๋ฅผ ์ ๋ ฌํ ์ ์๋ค.
*Arrays.sort() ๊ฐ์ ๊ฒฝ์ฐ๋ ๋ฐฐ์ด์ ์ ๋ ฌํ๋ค. ๋ฐ๋ผ์ ๋ฐฐ์ด์์ ๊ฐ์ฒด๋ฅผ ๋ฃ์ด์ Arrays.sort()ํด๋ ์ ๋ ฌ์ด ๋๋ค.
import java.util.*;
class Point implements Comparable<Point> {
public int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Point o) {
if(this.x == o.x) return this.y - o.y;
else return this.x - o.x;
}
}
class Main{
public static void main(String[] args) {
Main T = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Point[] arr = new Point[n];
for(int i = 0; i < n; i++) {
int x = in.nextInt();
int y = in.nextInt();
arr[i] = new Point(x, y);
}
Arrays.sort(arr);
for(Point x : arr) System.out.println(x.x + " " + x.y);
}
}
์ ๋ฆฌํ์๋ฉด Collection.sort๋ ๋ฆฌ์คํธ๋ฅผ ์ ๋ ฌ Arrays.sort๋ ๋ฐฐ์ด์ ์ ๋ ฌํ๋ค. ๊ทธ๋ฆฌ๊ณ wrapper ํด๋์ค๋ ์ผ๋ฐ์ ์ผ๋ก
Comparable<T> ๋ฅผ implements ํ๊ณ ์๊ณ ๋ง์ฝ ์๋ก์ ํด๋์ค๋ฅผ ์ ๋ คํ๊ธฐ ์ํด์๋ Comparable<T>๋ฅผ implements ํ๊ณ
comparTo๋ฅผ ์ฌ์ ์ ํด์ค์ผ ํ๋ค.
์ค๋๋ ๋ฐฐ์๊ฐ๋ค~~
'์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ฒฐ์ ์๊ณ ๋ฆฌ์ฆ] 9. ๋ฎค์ง๋น๋์ค (1) | 2024.01.24 |
---|---|
[์ ๋ ฌ] 8. ์ด๋ถ๊ฒ์ (0) | 2024.01.20 |
[์ ๋ ฌ] 6. ์ฅ๋๊พธ๋ฌ๊ธฐ (2) | 2024.01.19 |
[์ ๋ ฌ] 5. ์ค๋ณต ํ์ธ (0) | 2024.01.18 |
[์ธํ๋ฐ_์ ๋ ฌ] 4. Least Recently Used (0) | 2024.01.18 |
๋๊ธ