2014년 5월 22일 목요일

자바 개선된 for 루프(Java ForLoop) 예제,[자바JSP/Servlet/JDBC/MiPlatform/XPlatform/교육/JSP/서블릿강좌/Spring교육잘하는곳/자바,JSP/spring/교육추천/JSP/JDBC실무교육]

자바 개선된 for 루프(Java ForLoop) 예제,[자바JSP/Servlet/JDBC/MiPlatform/XPlatform/교육/JSP/서블릿강좌/Spring교육잘하는곳/자바,JSP/spring/교육추천/JSP/JDBC실무교육]

자바 개선된 for 루프(Java ForLoop) 예제, 자바공부 열심히 하세요. 책도 많이 보시구요,

아래 예제 및 결과를 참고 하세요...

package onj;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ForTest {
static int[] squares = { 0, 1, 4, 9, 16, 25 };
private static List arrayList = new ArrayList();

private static void fill() {
for (int i = 0; i < 5; i++) {
arrayList.add(i);
}
}

static void forDisplay1() {
for (int i = 0; i < squares.length; i++) {
System.out.printf("%d squared is %d.\n", i, squares[i]);
}
}

static void forDisplay2() {
int j = 0;
for (int i : squares) {
System.out.printf("%d squared is %d.\n", j++, i);
}
}

static void arrayListDisplay1() {
for (int i = 0; i < arrayList.size(); i++) {
System.out.printf("%d arrayList is %d.\n", i, arrayList.get(i));
}
}

static void arrayListDisplay2() {
Iterator iterator = arrayList.iterator();
int j = 0;
for (; iterator.hasNext();) {
System.out.printf("%d arrayList is %d.\n", j++, iterator.next());
}
}
static void arrayListDisplay3() {
int j = 0;
for (Object i : arrayList) {
System.out.printf("%d arrayList is %d.\n", j++, i);
}
}

public static void main(String[] args) {
forDisplay1();
System.out.println("---------------------");
forDisplay2();
System.out.println("---------------------");

fill();
arrayListDisplay1();
System.out.println("---------------------");
arrayListDisplay2();
System.out.println("---------------------");
arrayListDisplay3();
}
}



[결과]

0 squared is 0.
1 squared is 1.
2 squared is 4.
3 squared is 9.
4 squared is 16.
5 squared is 25.
---------------------
0 squared is 0.
1 squared is 1.
2 squared is 4.
3 squared is 9.
4 squared is 16.
5 squared is 25.
---------------------
0 arrayList is 0.
1 arrayList is 1.
2 arrayList is 2.
3 arrayList is 3.
4 arrayList is 4.
---------------------
0 arrayList is 0.
1 arrayList is 1.
2 arrayList is 2.
3 arrayList is 3.
4 arrayList is 4.
---------------------
0 arrayList is 0.
1 arrayList is 1.
2 arrayList is 2.
3 arrayList is 3.
4 arrayList is 4.

댓글 없음:

댓글 쓰기