본문 바로가기

빅데이터과정/JAVA

#55_140829_JAVA_FOR EACH

728x90

# 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,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,0,0,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,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,0,8,8,8,8,8,8,8,7,8,8,8,8,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,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,7,8,7,8,8,8,8,7,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,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,7,7,8,8,8,8,8,8,8,7,8,8,8,7,8,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,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,8,8,8,8,8,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,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