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

前端 未结 4 2007
一整个雨季
一整个雨季 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 12:02

    You'll need to either iterate with an integer index, or keep a counter going.

    List aList;
    ...
    int index = 0;
    for(kind x  :  aList)
    {
       ...
       index++;
    }
    

    or

    for(int index = 0; index < aList.size(); index++)
    {
        Kind x = aList.get(index)
    ...
    }
    

提交回复
热议问题