프로그래머스/lv0
배열 원소의 길이
do_it0904
2022. 12. 5. 09:58
/* 배열 원소의 길이
* 문자열 배열 strlist가 매개변수로 주어집니다.
* strlist 각 원소의 길이를 담은 배열을 retrun
*
* strlist result
* ["We", "are", "the", "world!"] [2, 3, 3, 6]
* ["I", "Love", "Programmers."] [1, 4, 12]
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class programmer_0_62 { static String[] a1 = {"We", "are", "the", "world!"}; static String[] a2 = {"I", "Love", "Programmers."}; public int[] solution(String[] strlist) { int[] answer = new int[strlist.length]; for(int i = 0; i < strlist.length; i++){ answer[i] = strlist[i].length(); } for(int i : answer) System.out.println(i); return answer; } public static void main(String args[]){ programmer_0_62 t = new programmer_0_62(); System.out.println("---------------------------------------"); System.out.println("result = " + t.solution(a1)); System.out.println("---------------------------------------"); // System.out.println("result2 = " + t.solution(a2)); // System.out.println("---------------------------------------"); } } | cs |