코딩 테스트에서 자주 사용되는 Java API 를 작성 해본다. 원래 항상 코딩 테스트 일정이 잡히면 엄청 급하게 외우기 식으루 주먹구구식으로 했었는데 이제야 좀 정리해보고 이해할 정도에 여유가 생겨서 도전 해본다. 대부분의 내용은 자바 API 이곳에서 참고하고 적었다.
Scanner
Scanner scanner = new Scanner(System.in);
String str1 = scanner.next();
String str2 = scanner.nextLine();
double v = scanner.nextDouble();
int i = scanner.nextint();
보통 입출력으로 문제를 주는 문제에서 많이 사용한다. C++ 에서는 cin >> 등으로 입출력을 해결 했지만 자바에서는 Scanner 클래스를 사용한다.
.next() 의 경우 공백이 나올때까지 읽고, .nextLine() 의 경우 Enter 가 나올 때까지 읽는다.
charAt()
String str = "Hello World";
char c = str.charAt(0);
System.out.print(c);
String str = "0";
int n = str.charAt(0) - '0'; //0
String 데이터에서 특정 문자를 얻고 싶을때는 charAt() 을 사용하면 된다. 추가적으로, 숫자로 변환하고 싶다면 - '0'을빼주면 된다.
대소문자
String str = "hello World";
str = str.toUpperCase() ; //대문자로 변환
str = str.toLowerCase() ; //소문자로 변환.
Char c = "c";
c = Character.toUpperCase();
c = Character.toLowerCase();
char c = 'c';
Character.isLowerCase(c); //true;
Character.isUpperCase(c) //false;
toCharArray
String str = "hello world";
char[] chars = str.toCharArray();
String s = new String(chars); //"hello world";
String s1 = String.valueOf(chars) //"hello World";
알파벳인지 확인 - Character.isAlphabetic()
String str = "hello World";
char[] chars = str.toCharArray();
for (char c : chars){
System.out.println(Character.isAlphabetic(c));
}
split()
문자열을 잘라서 배열로 만들어준다.
String answer = "Hello World";
String[] s = answer.split(" ");
answer = "Hello,World";
String[] split = answer.split(",");
SubString()
public String substring(int beginIndex, int endIndex);
String str = "Hello World";
str.substring(5); // (공백)World
str.substring(6,11); //World
StringBuilder
문자열을 이어붙이거나 거꾸로 출력하기 위해서 사용되는 함수
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" World");
String s = sb.toString(); //hello world
//역순 출력
String = "Hello";
StringBuilder sb = new StringBuilder(str);
String s = sb.reverse().toString();
채우기
int[] arr = new int[10];
Arrays.fill(arr,Integer.MAX_VALUE);
//int 로 해두면전부 0으로 세팅되는데 fill 방법으로 값을 세팅 해주었다.
정렬
int[] ints = new int[3];
int[0] = 6;
int[1] = 3;
Arrays.sort(ints);
for(int n : ints){
System.out.println(n);
}
clone
int[] ints = new int[3];
int[0] = 1;
int[1] = 2;
int[2] = 3;
int[] clone = ints.clone();
min, max
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(0,3,2,1,5));
int max = Collections.max(list);
int min = Collections.min(list);