Does Java's foreach loop preserve order?

前端 未结 2 1064
失恋的感觉
失恋的感觉 2021-01-01 08:41

Does Java\'s foreach loop start at the first object and in a linear fashion work it\'s way to the end? For example

String[] names = new String[] {\"Zoe\", \"         


        
2条回答
  •  轮回少年
    2021-01-01 08:59

    Yes. The order is not changed. This applies to all types of collections of the Java Collection Framework implementing the iterator interface that is used by the for-loop. If you want to sort your Array, you can use Arrays.sort(names)

提交回复
热议问题