Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reverse Iteration of enum 1

Status
Not open for further replies.

uolj

Programmer
Jul 2, 2003
529
GB
Code:
public class EnumTest {

    public enum MyEnum {
        One,
        Two,
        Three
    }
    public static void main(String[] args) {
        for (MyEnum e : MyEnum.values()) {
            System.out.println(e);
        }
    }
}
The code above outputs the string representation of each member of the enum in declaration order.

What is the most elegant way to change the foreach loop to go in reverse order?

I was thinking about using Collections.reverse, but have been unable to come up with a clean way to use it. Any help would be appreciated.
 
Code:
public static void main(String[] args) 
{
	Stack <MyEnum> s = new Stack <MyEnum> ();

	for (MyEnum e : MyEnum.values())
	{
	    s.push (e);
	}

	while (! s.empty ())
	{
		System.out.println (s.pop ());
	}

}

seeking a job as java-programmer in Berlin:
 
The other way to do it is with recursion.... A little longer to code and I'm not sure which would be more effecient (my guess: recursion would use memory, using the stack would use less time).

[plug=shameless]
[/plug]
 
Code:
for (int i = MyEnum.values().length - 1; i >= 0; --i) {
    MyEnum e = MyEnum.values()[i];
    System.out.println(e);
}
I ended up using the loop above. Simple, no extra copying, small amount of code. I was hoping there was a ReverseIterator built-in, or some way to reverse the array without modifying it or copying it, but it doesn't look like such a method exists.

If I need to do this a lot I'll probably implement or borrow a ReverseIterator that implements Iterable so I can use it in the foreach loop.

Thanks for the input.
 
Just a performance issue

Code:
enum values = MyEnum.values();
int len = values.length -1;
for (int i = len; i >= 0; --i) {
    System.out.println(values[i]);
}

I know, Iknow ... but I was bored :p

Cheers,

Dian
 
Are you saying that the call to values() inside the loop will incur function call overhead causing decreased performance? Other than that, I don't see any performance gain in your version, and I would guess (but don't know) that with a static enum the function call could get optimized away as easily as the extra local variables your code uses.

Just a guess though. :)
 
Well a function call is always going to be slower than a simple int eval. Having said that, the compiler may do that optimization for you - but I'm not sure ...

--------------------------------------------------
Free Database Connection Pooling Software
 
In fact I was bored, as I said.

There are three performance improvements:

1.- The values() function is just called once, not in every iteration of the loop.

2.- The length function is just called once, not in every iteration of the loop.

3.- The creation of a MyEnum object in every iteration of the loop is avoided.

Again, don't give it too much importance,

Cheers,

Dian

 
Dian,

No worries. I am actually pretty new to Java, and so I really just want to verify my understanding or assumptions about what is going on.

I'll grant you #1, that makes sense.

For #2, I was assuming that length was not a function at all, and would therefore not incur any more of a performance hit than an extra local variable that stored its value. Please let me know if this assumption is incorrect.

As for #3, of course, that part could have been removed from my code. I was just leaving it in there to provide the same functionality as the foreach loop achieves, which also provides a temporary variable to use inside the loop. But you are right that it isn't necessary to use the extra variable in that simple code.

Thanks!
 
Sorry, you're right on #2.

It's just a field access, the optimization is just that you save a -1 in every iteration :)

The problems is I was thinking on the lenght() method that some classes have.

Apologies.

Diancecht.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top