List vs List iterator

后端 未结 6 1069
清酒与你
清酒与你 2021-01-06 02:51

I have one list:

List myList = new ArrayList();


To get from this list there are two methods:

1.

<
6条回答
  •  猫巷女王i
    2021-01-06 03:43

    They do the same thing - the enhanced for loop is just syntactic sugar for the longhand version (for iterables; for arrays it's slightly different). Unless you need the iterator explicitly (e.g. to call remove()) I'd use the first version.

    See section 14.14.2 of the Java Language Specification for more details of the exact transformation performed by the compiler.

提交回复
热议问题