Note/JAVA

String Array 예제

sungwonpekr 2013. 5. 22. 11:19


class Foo {
  public static void main(String args[]) {

    // s 라는 문자열 배열 선언과 초기화
    String s[] = { "Google", "Yahoo", "MSN", "땡칠이" };

    // 배열의 모든 요소 화면에 출력
    for (int i = 0; i < s.length; i++)
      System.out.println(s[i]);



/////////////////////////////////////////////////////////////////



    // 이렇게 하면 알아보기 쉽게 초기화할 수 있음
    String names[] = {  "맹구",
                      "배용준",
                      "땡칠이",
                      "장동건",
                      "강수정",
                      "송창식",
                      "황당해",
                      "고은아"};

   // 배열의 모든 요소 화면에 출력
   for (int i = 0; i < names.length; i++)
     System.out.println(names[i]);



/////////////////////////////////////////////////////////////////



    // new 연산자로, 10개의 문자열 오브젝트 미리 확보하기
    String cool[] = new String[10];
    for (int i = 0; i < cool.length; i++) {
      cool[i] = "ㅎㅎ"; // 모든 요소들을 "ㅎㅎ" 라는 문자열로 채우고
      System.out.format("%d = %s%n", i, cool[i]); // 출력하기
    }


  }
}