Name: rmT116609 Date: 06/02/2004
A DESCRIPTION OF THE REQUEST :
Using the new enhanced for-loop to iterate over an enum is very nice and clean. However, when needing to iterate in reverse, extra effort and overhead is needed. It would be nice if there was a method to match the convenience of Enum.values()
JUSTIFICATION :
To make alternate uses of the enum pattern clean and consistent
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
enum Coin {Penny,Nickle,Dime,Quarter}
for(Coin coin : Coin.reverse())
System.out.println("Coin: " + coin);
ACTUAL -
Have to do the reversal by hand.
---------- BEGIN SOURCE ----------
import java.util.*;
public class revEnumVals
{
public enum Chip
{
Penny(1),Nickle(5),Dime(10),Quarter(25),Dollar(100);
Chip(int val){this.val = val;}
protected int val;
public int getValue(){return val;}
}
public static void main(String[] args)
{
System.out.println("In order:");
for(Chip chip : Chip.values())
System.out.println(chip + " = " + chip.getValue());
System.out.println("\nIn reverse:");
Chip[] chips = Chip.values();
for(int i=chips.length-1; i>=0; i--)
System.out.println(chips[i] + " = " + chips[i].getValue());
System.out.println("========================================");
ArrayList<Chip> chipList = new ArrayList<Chip>();
Collections.addAll(chipList, Chip.values());
System.out.println("In order:");
for(Chip chip : chipList)
System.out.println(chip + " = " + chip.getValue());
System.out.println("\nIn reverse:");
Collections.reverse(chipList);
for(Chip chip : chipList)
System.out.println(chip + " = " + chip.getValue());
System.out.println("========================================");
List<Chip> chipsList = Arrays.asList(Chip.values());
System.out.println("In order:");
for(Chip chip : chipsList)
System.out.println(chip + " = " + chip.getValue());
System.out.println("\nIn reverse:");
Collections.reverse(chipsList);
for(Chip chip : chipsList)
System.out.println(chip + " = " + chip.getValue());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
1) don't use enhanced-for loop (ie: for(int i=..;i>=0;i--))
2) Create new list, Collections.addAll, Collections.reverse
3) Arrays.asList, Collections.reverse
(Incident Review ID: 275603)
======================================================================
A DESCRIPTION OF THE REQUEST :
Using the new enhanced for-loop to iterate over an enum is very nice and clean. However, when needing to iterate in reverse, extra effort and overhead is needed. It would be nice if there was a method to match the convenience of Enum.values()
JUSTIFICATION :
To make alternate uses of the enum pattern clean and consistent
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
enum Coin {Penny,Nickle,Dime,Quarter}
for(Coin coin : Coin.reverse())
System.out.println("Coin: " + coin);
ACTUAL -
Have to do the reversal by hand.
---------- BEGIN SOURCE ----------
import java.util.*;
public class revEnumVals
{
public enum Chip
{
Penny(1),Nickle(5),Dime(10),Quarter(25),Dollar(100);
Chip(int val){this.val = val;}
protected int val;
public int getValue(){return val;}
}
public static void main(String[] args)
{
System.out.println("In order:");
for(Chip chip : Chip.values())
System.out.println(chip + " = " + chip.getValue());
System.out.println("\nIn reverse:");
Chip[] chips = Chip.values();
for(int i=chips.length-1; i>=0; i--)
System.out.println(chips[i] + " = " + chips[i].getValue());
System.out.println("========================================");
ArrayList<Chip> chipList = new ArrayList<Chip>();
Collections.addAll(chipList, Chip.values());
System.out.println("In order:");
for(Chip chip : chipList)
System.out.println(chip + " = " + chip.getValue());
System.out.println("\nIn reverse:");
Collections.reverse(chipList);
for(Chip chip : chipList)
System.out.println(chip + " = " + chip.getValue());
System.out.println("========================================");
List<Chip> chipsList = Arrays.asList(Chip.values());
System.out.println("In order:");
for(Chip chip : chipsList)
System.out.println(chip + " = " + chip.getValue());
System.out.println("\nIn reverse:");
Collections.reverse(chipsList);
for(Chip chip : chipsList)
System.out.println(chip + " = " + chip.getValue());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
1) don't use enhanced-for loop (ie: for(int i=..;i>=0;i--))
2) Create new list, Collections.addAll, Collections.reverse
3) Arrays.asList, Collections.reverse
(Incident Review ID: 275603)
======================================================================