본문 바로가기

프로그래밍/알고리즘(자바)

서울에서김서방찾기 Level 1

서울에서김서방찾기 Level 1

https://programmers.co.kr/learn/challenge_codes/106/


1
2
3
4
5
6
7
8
9
10
11
12
 
    public String findKim(String[] seoul){
        //x에 김서방의 위치를 저장하세요.
        int x = 0;
    for(int i =0; i<seoul.length;i++){
            if(seoul[i].equals("Kim")){
             x=i;
          break;
       }
     }
    return "김서방은 "+ x + "에 있다";
}
 
cs


이 코드는


    public String findKim(String[] seoul){
        //x에 김서방의 위치를 저장하세요.
        int x = Arrays.asList(seoul).indexOf("Kim");

        return "김서방은 "+ x + "에 있다";
    }


이렇게 표현할 수 있다....


indexOf는 List(Arraylist)에서 쓰이는 인덱스 값을 찾는 메소드이다.