Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5056415

Need reverse() for Enum.values()

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Not an Issue
    • P4
    • None
    • 5.0
    • core-libs
    • x86
    • windows_xp

    Description

      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)
      ======================================================================

      Attachments

        Activity

          People

            martin Martin Buchholz
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: