공부/JAVA

배열 (향상된 for문 이용하기)

예림220 2023. 6. 24. 16:51

public class ArrayTest {

//ArrayTest 클래스 

public static void main(String[] args) {

//메인메서드 

 

int[] scoreList = {10, 20, 30, 40, 50}; 

//int 타입에 scoreList라는 배열 안에 10, 20, 30, 40, 50 넣기

 

for (int i=0; i<scoreList.length; i++) {

System.out.println(scoreList[i]);

}

 

// int i는 초기값이 0이고, 0부터 scoreList.length(=5)까지 반복하면서 scoreList[i]를 출력해주고 1번 끝날때마다 i에 1을 더해주라는 뜻

// scoreList[0] = 10, [1]=20 ...[4]=50

 

for(int score: scoreList) {

System.out.println(score);

}

//향상된 for문

//int score = int 타입에 score형태의 참조변수 

//scoreList 배열참조변수 

//scoreList 배열에 있는 값을 score에서 하나씩 출력한다는 뜻 

 

String[] stringList = {"가","나","다","라","마"};

for (int i=0; i<scoreList.length;i++) {

System.out.println(scoreList[i]);

}

//string 타입으로 배열 값 출력

 

for (String abc : stringList) {

System.out.println(abc);

}

// 향상된 for문 사용

}

 

}