잡화/코딩테스트
코딩 테스트 함수 정리[JAVA]
꾹꾹이
2022. 5. 31. 15:42
728x90
1. Arrays.copyOfRange() : 특정범위 배열복사
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import java.util.*;
public class Solution {
private static int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
public static void main(String[] args) {
int[] arr1 = Arrays.copyOfRange(arr, 2,6);
System.out.print("arr의 요소 중 인덱스2에서 5까지 불러오기 : ");
for(int i=0;i<arr1.length;i++) {
System.out.print(arr1[i]+" ");
}
}
}
|
cs |
결과: 2 3 4 5
2.Math.min, Math.max: 전달된 데이터 중 더 큰 수와 더 작은 수를 반환해주는 메소드
1
2
3
4
5
6
7
8
9
10
11
|
import java.lang.Math;
public class Sample
{
public static void main(String[] args)
{
System.out.println(Math.max(10,100)); // 100
System.out.println(Math.min(10,100)); // 10
}
}
|
cs |
결과: 100, 10
3.Integer.ParseInt(): 문자열을 숫자로 String->int
1
2
3
4
5
6
7
8
9
10
11
12
|
public class StringToInt {
public static void main(String[] args) {
String str1 = "123";
String str2 = "-123";
int intValue1 = Integer.parseInt(str1);
int intValue2 = Integer.parseInt(str2);
System.out.println(intValue1); // 123
System.out.println(intValue2); // -123
}
}
|
cs |
결과: 123, -123
4.Integer.toString(): 숫자를 문자열로 int->String
1
2
3
4
5
6
7
8
9
10
11
12
|
public class IntToString {
public static void main(String[] args) {
int intValue1 = 123;
int intValue2 = -123;
String str1 = Integer.toString(intValue1);
String str2 = Integer.toString(intValue2);
System.out.println(str1);
System.out.println(str2);
}
}
|
cs |
결과: 123, -123
5.StringBuilder: 문자열을 더할 때 사용
1
2
3
4
5
6
|
StringBuilder sb = new StringBuilder();
sb.append("ABC");
sb.append("DEF");
System.out.println(sb.toString());
|
cs |
결과: ABCDEF