[Programmers / JAVA] Level 1 Finding Mr. Kim in Seoul (12919)
[Programmers / JAVA] Level 1 Finding Mr. Kim in Seoul (12919)
Find the position x of "Kim" among the elements of the String array seoul, and complete the solution function that returns a String saying "Mr. Kim is at x". "Kim" appears only once in seoul, and there will be no invalid input.
@RWBwritten at 2021-12-17 09:33:21
| Rank | Language Used |
|---|---|
| Level 1 | 🖼️ JAVA |
Find the position x of "Kim" among the elements of the String array seoul, and complete the solution function that returns a String saying "Mr. Kim is at x". "Kim" appears only once in seoul, and there will be no invalid input.
- seoul is an array of length 1 to 1000, inclusive.
- The elements of seoul are strings of length 1 to 20, inclusive.
- "Kim" is guaranteed to be included in seoul.
| seoul | return |
|---|---|
| { "Jane", "Kim" } | "Mr. Kim is at 1" |
Unlike a plain array like String[], a List provides a method called indexOf() that returns the position of the desired element.
Use the Arrays.asList() method to convert the array into a List, then use indexOf() to get the position and use it to build and return the answer.
JAVA
import java.util.Arrays; /** * Finding Mr. Kim in Seoul class * * @author RWB * @since 2021.12.13 Mon 15:44:33 */ class Solution { /** * Method that returns the answer * * @param seoul: [String[]] strings * * @return [String] the answer */ public String solution(String[] seoul) { return new StringBuilder("김서방은 ").append(Arrays.asList(seoul).indexOf("Kim")).append("에 있다").toString(); } }
# Programmers# Algorithm# JAVA# Level 1
