728x90
반응형
ArrayList 오름차순 정렬
방법 1
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
|
import java.util.ArrayList;
import java.util.Collections;
public class Ascending_Sort {
public static void main(String[] args) {
ArrayList<Integer> data = new ArrayList<Integer>();
/* 임의로 데이터 삽입 */
data.add(3);
data.add(7);
data.add(5);
/* Default Ascending Sort */
Collections.sort(data);
output(data);
}
private static void output(ArrayList<Integer> data) {
int size = data.size();
for (int i = 0; i < size; i++) {
System.out.print(data.get(i) + " ");
}
System.out.println();
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
방법 2
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
29
30
31
32
33
34
35
36
37
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Ascending_Sort {
public static void main(String[] args) {
ArrayList<Integer> data = new ArrayList<Integer>();
/* 임의로 데이터 삽입 */
data.add(3);
data.add(7);
data.add(5);
Ascending ascending = new Ascending();
Collections.sort(data, ascending);
output(data);
}
private static void output(ArrayList<Integer> data) {
int size = data.size();
for (int i = 0; i < size; i++) {
System.out.print(data.get(i) + " ");
}
System.out.println();
}
}
/* 오름차순 정렬 */
class Ascending implements Comparator<Integer> {
@Override
public int compare(Integer a, Integer b) {
return a.compareTo(b);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
ArrayList 내림차순 정렬
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
29
30
31
32
33
34
35
36
37
38
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Descending_Sort {
public static void main(String[] args) {
ArrayList<Integer> data = new ArrayList<Integer>();
/* 임의로 데이터 삽입 */
data.add(3);
data.add(7);
data.add(5);
Descending descending = new Descending();
Collections.sort(data, descending);
output(data);
}
private static void output(ArrayList<Integer> data) {
int size = data.size();
for (int i = 0; i < size; i++) {
System.out.print(data.get(i) + " ");
}
System.out.println();
}
}
/* 내림차순 정렬 */
class Descending implements Comparator<Integer> {
@Override
public int compare(Integer a, Integer b) {
return b.compareTo(a);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
728x90
반응형
'자바(JAVA)' 카테고리의 다른 글
[Eclipse] Java Compiler Version 확인 및 변경하기 (0) | 2022.01.19 |
---|---|
[Eclipse] Properties 글자 색 변경 (0) | 2019.07.07 |
[Eclipse] 테마 강제 변경 (0) | 2019.07.07 |
[Java] ArrayList Object 오름차순, 내림차순 Example (0) | 2019.07.07 |
[Java] OOP 객체지향프로그래밍 개념 (0) | 2019.06.29 |