Iterating through a variable length array

前端 未结 4 1429
囚心锁ツ
囚心锁ツ 2020-12-03 13:54

How do I iterate over a Java array of variable length.

I guess I would setup a while loop, but how would I detect that I have reached the end of the array.

4条回答
  •  借酒劲吻你
    2020-12-03 14:20

    Arrays have an implicit member variable holding the length:

    for(int i=0; i

    Alternatively if using >=java5, use a for each loop:

    for(Object o : myArray) {
        System.out.println(o);
    }
    

提交回复
热议问题