서울에서김서방찾기 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)에서 쓰이는 인덱스 값을 찾는 메소드이다.
'프로그래밍 > 알고리즘(자바)' 카테고리의 다른 글
평균구하기 Level 1 : For each문 (0) | 2018.05.06 |
---|---|
문자열 내림차순으로 배치하기 Level 1 #Collections,join,sort,asList,reverse (0) | 2018.05.06 |
짝수와 홀수 Level 1 (0) | 2018.05.05 |
행렬의 덧셈 Level 1 (0) | 2018.05.05 |
나누어 떨어지는 숫자 배열 Level 1 (0) | 2018.05.05 |