# FOR EACH
for each | |
class EnhancedFor { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; int sum = 0;
for (int e : arr) sum += e;
System.out.println("배열 요소의 합: " + sum);
for (int e : arr) { System.out.print(e + " "); } } } | |
결과값 | 배열 요소의 합: 15 1 2 3 4 5 |
for each | |
class ScannerEx2 { public static void main(String[] args) throws Exception { int sum = 0; int sum1 = 0; int[][] arr={ {8,8,8,8,8,8,8,7,8,8,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}, {8,8,8,8,8,8,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,0,8,8,8,8,8,8,8,8,8,8,7,8}, {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}, {8,8,8,7,8,8,7,8,8,8,7,8,8,8,8,8,8,8,8,8,7,8,8,8,8,8,8,8,7,8,0,8,8,8,8,8,7,8,8,7}, {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}, {8,8,8,7,8,8,8,8,8,8,8,8,8,8,8,7,8,8,7,8,8,8,8,7,8,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8}, {8,8,8,8,8,8,8,8,8,8,8,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,7,8,8,8,8,8,8}, {8,8,8,8,0,0,8,7,8,8,7,7,8,8,7,8,8,7,7,8,7,8,8,8,8,8,8,8,8,8,8,7,8,8,8,8,7,7,0,8}, {8,8,8,8,0,0,0,8,7,8,8,8,7,8,8,8,8,8,7,7,7,8,8,8,8,8,8,8,8,8,8,0,8,8,8,8,8,8,0,8}, {8,8,8,7,8,8,0,0,0,0,0,7,7,7,8,8,8,8,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,7,8,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8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}, {8,8,8,8,7,8,7,7,8,8,7,7,7,8,8,8,8,8,7,8,8,7,8,8,8,8,8,7,7,8,8,7,8,8,8,8,8,8,8,8}, {8,8,8,7,8,0,8,8,8,8,8,8,8,8,8,8,8,8,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,0,0,0,8}, {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}, {8,7,8,7,8,8,8,7,8,8,8,8,8,8,7,7,8,8,8,8,8,8,8,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,7}, {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}, {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}};
System.out.println("배열의 세로길이 : "+arr.length);
for (int i=0;i<arr.length;i++) for (int e : arr[i]) { sum += e; } System.out.println("sum = "+sum);
for(int[] e : arr) for(int e2 : e) sum1 +=e2; //for each는 각각의 값을 arr의 길이만큼 반복한다 //int[] e는 2차원 배열에 대한 참조변수이고 e2는 e의 각 행 하나하나의 값이다 System.out.println("sum1 = "+sum1); } }
| |
결과값 | 배열의 세로길이 : 29 sum = 8998 sum1 = 8998 |
'빅데이터과정 > JAVA' 카테고리의 다른 글
#55_140902_JAVA_행번호 보이게 하기 (0) | 2014.09.02 |
---|---|
#55_140829_JAVA_상속 (0) | 2014.08.29 |
#55_140829_JAVA_배열 (0) | 2014.08.29 |
#55_140829_JAVA_SCANNER CLASS (0) | 2014.08.29 |
#55_140829_JAVA_SYSTEM CLASS (0) | 2014.08.29 |