티스토리 뷰

JAVA

for each

seoca 2019. 7. 13. 06:19

 

 

for-each 

 

for (Type obj : Collection) {...}



example

for
 (CustomInfo c : customManagers) {
   System.out.println(c.toString());
}

 

works only if the collection holds elements of type Type. 

 

 

 

public class Main{

    public static void main(String[] args) {

        int[] arr = new int[]{ 1,2,3,4,5 };

        //for-each. normally used for iterating Arrays or Collection

 

        for (int i : arr)

        {

            System.out.print(i); //12345

        }

        System.out.println("");

        //normal for loop

 

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

            int var = arr[i];

            System.out.print(var); //12345

        }

    }

}

 

 

'JAVA' 카테고리의 다른 글

Error: could not find or load main class Main  (0) 2019.07.19
Serialization example code in java  (0) 2019.07.17
Returning ArrayList from Method  (0) 2019.07.13
Iterator  (0) 2019.07.12
Check only integer input  (0) 2019.07.12