인프런 커뮤니티 질문&답변
for each 문을 사용하는 이유가 궁금합니다.
작성
·
320
0
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] str = s.split(" ");
int longIndex = 0;
for(int i = 1; i < str.length; i++) {
if(str[longIndex].length() < str[i].length()) {
longIndex = i;
}
}
System.out.println(str[longIndex]);
}
}
for문을 사용하면 더 간단한 것 같은데 for each문을 쓰는 특별한 이유가 있는지 궁금합니다.





