Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

KSI일기장

배열의 각 원소의 길이 출력(자바) 본문

JavaAlgorithm

배열의 각 원소의 길이 출력(자바)

MyDiaryYo 2023. 8. 8. 16:10

 

배열에서 각 원소의 문자열 길이를 출력해 보겠습니다.

 

public class LengthOfArray {

	public static void main(String[] args) {

		String[] strlist = {"we", "are", "the", "world"};
		
		int[] answer = new int[strlist.length];
		
		for(int i = 0; i < strlist.length; i++) {
			answer[i] = strlist[i].length();
			}
		
		//answer의 값들을 꺼내기 위한 향상된 for문
		for(int result : answer ) {
			System.out.print(result + " ");
		}
		
	}
}