✔ For 문 아래 Document 참고
https://www.w3schools.com/java/java_for_loop.asp
Java For Loop
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated
docs.oracle.com
✔ For-each 문 아래 Document 참고
https://www.w3schools.com/java/java_foreach_loop.asp
Java For-Each Loop
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
The For-Each Loop
The For-Each Loop Iterating over a collection is uglier than it needs to be. Consider the following method, which takes a collection of timer tasks and cancels them: void cancelAll(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) i.next().can
docs.oracle.com
# for문의 구성?
1. initializer(초기화)
2. condition(조건)
3. Increment/decrement(증감연산자)
로 이루어 져있다.
↓↓↓예시를 한번 보자↓↓↓
for (int i = 0; i < 5; i++) {
System.out.println("Hello world!");
}
for문안에 들어 있는것들을 설명을 해보자면,
int i = 0 → initializer(초기화)
i < 5 → condition(조건)
i++ → Increment/decrement(증감연산자)
# for-each문의 구성?
1. item(아이템)
2. array(배열)
# for-each는 언제 쓰나?
Array에 있는 요소(element)들을
앞전에 for문에서 썼던,
initializer(초기화), condition(조건), Increment/decrement(증감연산자)없이
하나하나 뽑고 싶을때.
↓↓↓예시를 한번 보자↓↓↓
String[] users = {"Leo", "Vivian", "David"};
for (String user : users) {
System.out.println(user);
}
/* 결과
Leo
Vivian
David
*/
Tip ✔
아래 코드를 모두 복사 혹은 다운로드 → IDE 에 붙여넣기 한 다음,
실행해보고 이리저리 코드를 수정해가면서
에러가 뜨면, 왜 에러가 뜨는지 직접 몸소 체험을 해봐야 경험치가 쌓입니다!
이것마저 못하는 게으름뱅이는 코딩 ㄹㅇ 접어야함 ㅅㄱ
아래 댓글창에 질문 언제든지 해주세요!
(시간되는 대로 답변해드리겠습니다)
public class ArrayLoopDemo {
public static void main(String[] args) {
String[] members = {"Jeff", "Sa", "Joo"};
for (int i = 0; i < members.length; i++) {
String member = members[i];
System.out.println(member + " is served!");
}
System.out.println();
// While의 불편함
// ->(Initializer, Condition, Increment/Decrement가 각각 떨어져 있음)을 개선하기 위해 For을 만듬!
// Array(배열)과 For의 조합을 쉽게 사용하기 위해서 For-Each를 만듬!
String[] members2 = {"Leo", "Vivian", "Wen"};
for (String e : members2) {
System.out.println(e + " is served!");
}
}
}
/* 결과:
Jeff is served!
Sa is served!
Joo is served!
Leo is served!
Vivian is served!
Wen is served!
*/
↓↓↓↓ 코드 다운로드 ↓↓↓↓
'🖥️프로그래밍 언어 > Java' 카테고리의 다른 글
[JAVA] 자바 정적변수(Static variables) = 클래스변수(Class variables) (0) | 2022.09.13 |
---|---|
[JAVA] 자바 Scanner + hasNextInt() (0) | 2022.09.12 |
[JAVA] 자바 메소드(Method) + while문 + 예시 (2) | 2022.09.11 |
[JAVA] 자바 배열(Array) + 예시 (2) | 2022.09.08 |
[JAVA] 자바 데이터 형 변환(Type Conversion) + 예시 (0) | 2022.09.06 |