How does the enhanced for statement work for arrays, and how to get an iterator for an array?

后端 未结 13 731
走了就别回头了
走了就别回头了 2020-11-27 04:59

Given the following code snippet:

int[] arr = {1, 2, 3};
for (int i : arr)
    System.out.println(i);

I have the following questions:

13条回答
  •  离开以前
    2020-11-27 05:36

    For (2), Guava provides exactly what you want as Int.asList(). There is an equivalent for each primitive type in the associated class, e.g., Booleans for boolean, etc.

        int[] arr={1,2,3};
        for(Integer i : Ints.asList(arr)) {
          System.out.println(i);
        }
    

提交回复
热议问题