2014년 1월 28일 화요일

자바 개선된 for 루프(Java ForLoop) 예제, JAVA FOR LOOP 자바공부 열심히 하세요. 책도 많이 보시구요, 아래 예제 및 결과를 참고 하세요... 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. [출처] 오라클자바커뮤니티 - http://www.oraclejavanew.kr/bbs/board.php?bo_table=LecJava&wr_id=670 자바 오라클/빅데이터 아이폰/안드로이드 닷넷/WPF 표준웹/HTML5 채용/취업무료교육 초보자코스 [기업100%환급]자바기초에서 JDBC, Servlet/JSP까지 총 5일 40시간 02-05 [기업100%환급]Spring ,MyBatis,Hibernate실무과정 총 5일 40시간 02-24 자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지 총 24일 72시간 02-06 Spring3.X, MyBatis, Hibernate실무과정 총 12일 36시간 02-11 Spring3.X, MyBatis, Hibernate실무과정 총 5일 35시간 02-15 자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지 총 10일 70시간 02-15 JAVA&WEB프레임워크실무과정 총 14일 98시간 03-08 [기업100%환급]SQL기초에서 Schema Object까지 총 5일 40시간 02-10 [기업100%환급]PL/SQL,ORACLE HINT,TUNING 총 5일 40시간 02-17 SQL초보에서실전전문가까지 총 18일 54시간 02-11 SQL초보에서실전전문가까지 총 8일 56시간 02-22

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

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

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.

댓글 없음:

댓글 쓰기