티스토리 뷰

Java

While / For Loop

techbard 2016. 7. 13. 11:25



import java.util.ArrayList;

import java.util.Iterator;


public class Main {


    public static ArrayList<String> al = new ArrayList<>();


    public static void main(String[] args) {


al.add("a");

al.add("b");

al.add("c");


// enhancedForLoop();


// iteratorForLoop();


// iteratorWhileLoop();


// whileLoopIndex();


    }


    private static void iteratorWhileLoop() {

Iterator<String> itr = al.iterator();

while (itr.hasNext()) {

   System.out.println(itr.next());

}

    }


    private static void iteratorForLoop() {

Iterator<String> itr = al.iterator();

for (; itr.hasNext();) {

   String str = itr.next();

   System.out.println(str);

}

    }


    private static void enhancedForLoop() {

for (String str : al) {

   System.out.println(str);

}

    }


    private static void whileLoopIndex() {

String[] strArr = new String[10];


// initialize the array

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

   strArr[i] = String.valueOf(i);

}


int idx = 0;

while (idx < strArr.length) {

   String str = strArr[idx++];

   System.out.println(str);

}

    }

}


댓글
댓글쓰기 폼
공지사항
Total
410,768
Today
37
Yesterday
44
«   2019/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
글 보관함