Iterating through a variable length array

前端 未结 4 1427
囚心锁ツ
囚心锁ツ 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:04
    for(int i = 0; i < array.length; i++)
    {
        System.out.println(array[i]);
    }
    

    or

    for(String value : array)
    {
        System.out.println(value);
    }
    

    The second version is a "for-each" loop and it works with arrays and Collections. Most loops can be done with the for-each loop because you probably don't care about the actual index. If you do care about the actual index us the first version.

    Just for completeness you can do the while loop this way:

    int index = 0;
    
    while(index < myArray.length)
    {
      final String value;
    
      value = myArray[index];
      System.out.println(value);
      index++;
    }
    

    But you should use a for loop instead of a while loop when you know the size (and even with a variable length array you know the size... it is just different each time).

    0 讨论(0)
  • 2020-12-03 14:04

    here is an example, where the length of the array is changed during execution of the loop

    import java.util.ArrayList;
    public class VariableArrayLengthLoop {
    
    public static void main(String[] args) {
    
        //create new ArrayList
        ArrayList<String> aListFruits = new ArrayList<String>();
    
        //add objects to ArrayList
        aListFruits.add("Apple");
        aListFruits.add("Banana");
        aListFruits.add("Orange");
        aListFruits.add("Strawberry");
    
        //iterate ArrayList using for loop
        for(int i = 0; i < aListFruits.size(); i++){
    
            System.out.println( aListFruits.get(i) + " i = "+i );
             if ( i == 2 ) {
                    aListFruits.add("Pineapple");  
                    System.out.println( "added now a Fruit to the List "); 
                    }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 14:20

    Arrays have an implicit member variable holding the length:

    for(int i=0; i<myArray.length; i++) {
        System.out.println(myArray[i]);
    }
    

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

    for(Object o : myArray) {
        System.out.println(o);
    }
    
    0 讨论(0)
  • 2020-12-03 14:28

    You've specifically mentioned a "variable-length array" in your question, so neither of the existing two answers (as I write this) are quite right.

    Java doesn't have any concept of a "variable-length array", but it does have Collections, which serve in this capacity. Any collection (technically any "Iterable", a supertype of Collections) can be looped over as simply as this:

    Collection<Thing> things = ...;
    for (Thing t : things) {
      System.out.println(t);
    }
    

    EDIT: it's possible I misunderstood what he meant by 'variable-length'. He might have just meant it's a fixed length but not every instance is the same fixed length. In which case the existing answers would be fine. I'm not sure what was meant.

    0 讨论(0)
提交回复
热议问题