๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[JAVA ์ฝ๋ฉ ์ด๋ณด] ๋ฐฐ์ด ์ ๋ ฌ (์ค๋ฆ์ฐจ์, ๋ด๋ฆผ์ฐจ์) ๋ณธ๋ฌธ
๐ฆฅ ์ฝํ
/JAVA
[JAVA ์ฝ๋ฉ ์ด๋ณด] ๋ฐฐ์ด ์ ๋ ฌ (์ค๋ฆ์ฐจ์, ๋ด๋ฆผ์ฐจ์)
์ง์ง์ํ์นด 2023. 7. 16. 00:00728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ codechacha ๋์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
๐ ์ซ์ ๋ฐฐ์ด ์ ๋ ฌ
๐ ์ค๋ฆ์ฐจ์
๋ณ์ arr์ ์์๋ฅผ ๋ณ๊ฒฝํด์ฃผ๊ธฐ ๋๋ฌธ์ ๋ฆฌํด ๊ฐ์ ๋ค๋ฅธ ๋ณ์์ ํ ๋นํ ํ์๊ฐ ์์
import java.util.*;
import java.io.*;
// tip: each public class is put in its own file
public class main {
// tip: arguments are passed via the field below this editor
public static void main(String[] args) {
int[] arr = { 1, 26, 17, 25, 99, 44, 303 };
// ๋ณ์ arr์ ์์๋ฅผ ๋ณ๊ฒฝํด์ฃผ๊ธฐ ๋๋ฌธ์ ๋ฆฌํด ๊ฐ์ ๋ค๋ฅธ ๋ณ์์ ํ ๋นํ ํ์๊ฐ ์์
Arrays.sort(arr);
System.out.println("์ค๋ฆ์ฐจ์ : " + Arrays.toString(arr));
}
}
๐ ๋ด๋ฆผ์ฐจ์
import java.util.*;
import java.io.*;
// tip: each public class is put in its own file
public class main {
// tip: arguments are passed via the field below this editor
public static void main(String[] args) {
Integer[] arr = {1, 26, 17, 25, 99, 44, 303};
// ๋ณ์ arr์ ์์๋ฅผ ๋ณ๊ฒฝํด์ฃผ๊ธฐ ๋๋ฌธ์ ๋ฆฌํด ๊ฐ์ ๋ค๋ฅธ ๋ณ์์ ํ ๋นํ ํ์๊ฐ ์์
Arrays.sort(arr, Collections.reverseOrder());
System.out.println("๋ด๋ฆผ์ฐจ์ : " + Arrays.toString(arr));
}
}
๐ ๋ถ๋ถ ์ ๋ ฌ
์ ๋ ฌ ๋ฒ์์ ๋ํ ์ฒ์ index์ ๋ง์ง๋ง index ๋ง ์ ๋ ฌ
import java.util.*;
import java.io.*;
// tip: each public class is put in its own file
public class main {
// tip: arguments are passed via the field below this editor
public static void main(String[] args) {
Integer[] arr = {1, 26, 17, 25, 99, 44, 303};
// ๋ณ์ arr์ ์์๋ฅผ ๋ณ๊ฒฝํด์ฃผ๊ธฐ ๋๋ฌธ์ ๋ฆฌํด ๊ฐ์ ๋ค๋ฅธ ๋ณ์์ ํ ๋นํ ํ์๊ฐ ์์
Arrays.sort(arr, 0, 4);
System.out.println("๋ถ๋ถ์ ๋ ฌ : " + Arrays.toString(arr));
}
}
๐ ๋ฌธ์ ๋ฐฐ์ด ์ ๋ ฌ
์ํ๋ฒณ์ ์์คํค ๊ฐ์ ๋น๊ตํ์ฌ ์ ๋ ฌ
๐ ์ค๋ฆ์ฐจ์
import java.util.*;
import java.io.*;
// tip: each public class is put in its own file
public class main {
// tip: arguments are passed via the field below this editor
public static void main(String[] args) {
String[] arr = {"Apple", "Kiwi", "Orange", "Banana", "Watermelon", "Cherry"};
Arrays.sort(arr);
System.out.println("์ค๋ฆ์ฐจ์ : " + Arrays.toString(arr));
}
}
๐ ๋ด๋ฆผ์ฐจ์
import java.util.*;
import java.io.*;
// tip: each public class is put in its own file
public class main {
// tip: arguments are passed via the field below this editor
public static void main(String[] args) {
String[] arr = {"Apple", "Kiwi", "Orange", "Banana", "Watermelon", "Cherry"};
Arrays.sort(arr, Collections.reverseOrder());
System.out.println("๋ด๋ฆผ์ฐจ์ : " + Arrays.toString(arr));
}
}
๐ ๋ฌธ์์ด ๊ธธ์ด ์์
import java.util.*;
import java.io.*;
// tip: each public class is put in its own file
public class main {
// tip: arguments are passed via the field below this editor
public static void main(String[] args) {
String[] arr = {"Apple", "Kiwi", "Orange", "Banana", "Watermelon", "Cherry"};
// ๋ฌธ์์ด ๊ธธ์ด๋ก ๋น๊ตํ๋ Comparator๋ฅผ ๊ตฌํ
Arrays.sort(arr, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
});
// ์ ๋ค์ฏ์ค๊ณผ ๊ฐ์
// Arrays.sort(arr, (s1, s2) -> s1.length() - s2.length());
System.out.println("๋ฌธ์์ด ๊ธธ์ด ์์๋ก ์ ๋ ฌ : " + Arrays.toString(arr));
}
}
728x90
๋ฐ์ํ
'๐ฆฅ ์ฝํ > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA ์ฝ๋ฉ ์ด๋ณด] Queue (ํ) Method (0) | 2023.07.16 |
---|---|
[JAVA ์ฝ๋ฉ ์ด๋ณด] Stack (๋ฆฌ์คํธ) Method (0) | 2023.07.16 |
[JAVA ์ฝ๋ฉ ์ด๋ณด] List (๋ฆฌ์คํธ) Method (0) | 2023.07.16 |
[JAVA ์ฝ๋ฉ ์ด๋ณด] StringBuilder(์คํธ๋ง๋น๋) Method (0) | 2023.07.16 |
[JAVA ์ฝ๋ฉ ์ด๋ณด] String(์คํธ๋ง) Method (0) | 2023.07.16 |
Comments