toCharArray() 과 charAt() 차이

 구분 charAt() toCharArray()
 목적 한 글자만 필요할 때 전체 문자열을 한 번에 문자 배열로 다룰 때
 성능 작은 문자열엔 거의 차이 없음 반복문에서 성능이 더 좋을 수 있음 (char[]에 직접 접근하므로)
 메모리 문자열만 유지 char[] 배열을 추가로 생성함

 

사용방법

public class Test {
    public static void main(String[] args) {
        String test = "test!!";
        char c = test.charAt(1); // 'e'
        char[] chars = test.toCharArray();

        System.out.println("charAt : " + c);
        System.out.println("toCharArray : " + chars[1]) ; // 'e'

    }
}

 
 

결과

+ Recent posts