Yeon's Frame
  • Intro
  • Project
    • 미소웨더
      • 기획 - 아이디어 및 유스케이스 정리
      • DB 설계 - JPA 활용 중심
  • Study
    • Spring
      • Spring Feature
      • N+1
      • OSIV
      • @Valid
      • Spring Boot Feature
      • Spring 5 Feature
      • JPA vs MyBatis
      • Filter, Interceptor
      • Persistence Context
      • @Transactional
      • @Controlleradvice, @ExceptionHandler
      • Spring Security
      • Dispatcher Servlet
      • @EnableWebMvc
      • Stereo Type
      • AOP
      • JPA Repository
    • Infrastructure
      • Git
      • DNS
      • JWT
      • DevOps
      • Docker
      • Jenkins
      • Cloud Computing
      • MSA
    • Clean Code
      • 깨끗한 코드
      • 의미있는 이름
      • 함수
      • 주석
      • 형식 맞추기
      • 객체와 자료구조
      • 오류 처리
      • 경계
      • 단위테스트
      • 클래스
      • 시스템
  • Basic
    • Java
      • OOP
      • OOL Features
      • Class & Objects
      • Instance & Heap Memory
      • Constructor
      • Reference Type Uses
      • Access Modifier & Hiding
      • This
      • Collaboration
      • Static
      • Inheritance
      • Polimorphism & Casting
      • Abstract Class
      • Interface
      • Object Class
    • Java Coding
      • JVM
      • String, StringBuffer, StringBuilder
      • SOF
      • Blockcing/NonBlocking
      • Enum
      • Static
      • Thread
      • hashCode() | equals()
      • JDK8
      • Stream
      • Optional
      • Lambda & Closure
      • Exception
      • Garbage Collecter
      • Collection
      • Call by Value & Call by Reference
      • Generic
    • Java_Advance
      • Inner Class
      • Lambda Expression
      • Functional Interface
      • OOP vs Lambda Expression
      • Stream
      • Reduce()
      • Exception Handling
      • Custom Exception
      • Error Log
      • IO Stream
      • IO Stream - Serialization
      • File Class
      • Decorator Pattern
      • Thread
    • Data Structure
      • Intro
      • Generic(1)
      • Generic(2) T extends
      • Generic(3) Method
      • Collection Frameworks
      • List
      • Iterator
      • Set
      • Comparable & Comparator
      • Map
  • Practice
    • Algorithm
      • Strategy
        • Primitive, Reference, Generic
        • Number Data Types
        • For-Each
        • Array, Queue, ArrayDeque
        • Array vs ArrayList
Powered by GitBook
On this page
  • 기본 forans vs 향상된 for문
  • Priority Queue의 경우 주의 사항
  1. Practice
  2. Algorithm
  3. Strategy

For-Each

기본 forans vs 향상된 for문

# 기본 for문
for (int i = 0; i < al.size(); i++){
    int now = al.get(i);
    System.out.print(now)
}

# 향상된 for문
for (int i : al){
    System.out.println(i + " ");
}
  • 향상된 for문(for-each문)을 사용하면 코드가 간결해진다.

  • 속도나 메모리면에서 일반적인 for문과 큰 차이가 없다.

Priority Queue의 경우 주의 사항

// 
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(4);
pq.offer(5);
pq.offer(1);
pq.offer(2);
pq.offer(3);

for (int i : pq){
    System.out.print(i + " ");
}
while(!pq.isEmpty()) {
    System.out.print(pq.poll() + " ");
}

>>>
1 2 4 5 3
1 2 3 4 5 (정상적인 PQ 사용 결과)
  • 우선순위 큐 경우, poll로 꺼내야 정렬된 순으로 값을 얻을 수 있다.

그냥 큐를 쓰면 어떨까?

PreviousNumber Data TypesNextArray, Queue, ArrayDeque

Last updated 2 years ago