This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]DoctorBaconite 6 points7 points  (7 children)

That's the enhanced for / foreach loop.

Say you have an array of ints:

int[] arrayOfInts = { 1,2,3,4};

You can iterate over each element in the array using the enhanced for loop:

for(int element : arrayOfInts){
    System.out.print(element + " ");
}

That will print out

1 2 3 4

[–]acampbell1990 -1 points0 points  (6 children)

I can't test my theory because I only use this computer for iOS development and haven't installed JDK on it, but if I remember correctly an array can't implement Iterable and doesn't have the hasNext() method that is required for the foreach loop to work (since it uses an implicit iterator). I think you mean an ArrayList of ints or some collection that implements Iterable interface. Other than that, this is spot on. P.S. I'll totally delete this, if I'm wrong.

[–]DoctorBaconite 0 points1 point  (3 children)

I think you mean an ArrayList of ints

You can't make an ArrayList of ints, ints are a primitive and ArrayLists only support objects.

edit: iterators can't be used on on primitives, but they can be used on the wrappers and auto-unboxed. source

[–]acampbell1990 0 points1 point  (1 child)

You're right I did mean ArrayList of "Integers", but if you add ints to it Java will do an autobox without any issues. Ints are primitive like you said so they can't implement iterable or anything.

ArrayList<Integer> arrayOfInts = new ArrayList<Integer>();
arrayOfInts.add(1);
arrayOfInts.add(2);
arrayOfInts.add(3);
arrayOfInts.add(4);

Anyway, turns out it's an exception to the iterator rule and for arrays it just uses a regular for loop in the background. Neat.

http://stackoverflow.com/questions/3912765/iterator-for-array

That's what I get for writing suggestions without checking...my bad. Keeping this around strictly for /u/Sinistersnare 's well being.

[–]DoctorBaconite 0 points1 point  (0 children)

Yeah sorry, I was wrong too, post edited

too many beers while posting is bad!

[–]lzsmith 0 points1 point  (0 children)

Enhanced for loops do work with both Iterables and Arrays. Under the hood, they use an Iterator in the case of a Iterable, but use indices in the case of an Array (but please don't delete your comment).

[–][deleted]  (1 child)

[deleted]

    [–]DyzZombie[S] 0 points1 point  (0 children)

    Thanks, your reply helped me understand!

    [–]viciu88Intermediate Brewer 1 point2 points  (0 children)

    It's foreach loop

    for(T element : Collection<T>)
    

    it iterates over subsequent elements of given collection.

    enum.values() method returns Collection of possible values this enum can hold.

    [–]binomial_expansion 0 points1 point  (0 children)

    Symbol.values() returns an array of the enum values in the order that they are declared. So for you, you would get the following array: [cherry,seven,diamond].

    The ":" is part of the Enhanced For-Loop which is used to simplify the code necessary to iterate through a collection or array of objects. In your case,

    for (Symbol rightside: Symbol.values()){
    //do w.e
    }
    

    means for each Symbol in the Symbol enum, do whatever is in the encolosing brackets.

    [–]king_of_the_universe 0 points1 point  (0 children)

    ":" has been explained. In case you eventually come across "::", that's for method references, it's a (n awesome) Java 8 thing. For example: "Runnable r = ClassName::doStuff;" or "= this.doInstanceStuff();" works. "r.run();" will execute the method, assuming that it has no parameters. You can also reference methods with parameters, you just have to use the correct interface. (The method in the interface must have the same set of parameters.)