how to print the index number of elements in the ArrayList using for each looping

前端 未结 4 2039
一整个雨季
一整个雨季 2021-01-22 11:36

Can anybody tell me how to print the index number of elements in the ArrayList using for each looping in Java.

4条回答
  •  孤独总比滥情好
    2021-01-22 11:56

    like @Michael's but shorter. A pet obsession I'll admit.

    List list = Arrays.asList("zero", "one", "two", "three");
    int index=0;
    for(String s : list)
        System.out.println((index++)+": "+s);
    

    prints

    0: zero
    1: one
    2: two
    3: three
    

提交回复
热议问题