아래 예시처럼 Enhanced for문의 경우, 위의 문장에서 아래의 문장으로 번역된다.
https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.14.2
Chapter 14. Blocks and Statements
In the following example, a mathematical graph is represented by an array of arrays. A graph consists of a set of nodes and a set of edges; each edge is an arrow that points from some node to some other node, or from a node to itself. In this example it is
docs.oracle.com
그렇기에 list 같이 요소 접근에 O(n)의 시간복잡도를 가지는 자료구조를 for loop에서 이용할 때,
Enhanced for문을 사용하면 상수 시간으로 효율적인 접근이 가능해진다.
하지만, array 같은 자료구조를 사용할 때는 불필요한 연산이 늘어나 오히려 비효율적일 수 있다.
잘못된 내용에 대한 수정이나, 본문에 대한 어떠한 피드백도 환영하니 댓글로 남겨주시면 감사하겠습니다. ㅎ
'JAVA' 카테고리의 다른 글
[Java] Reflection 사용 시 주의점. (0) | 2022.08.28 |
---|