• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting previous element in Enumaration

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

How to get previous element in Enumaration.
Thanks,
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't. If you need access to the previous element then you need to declare a variable and point this to the current value before you call the next() method.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use the ordinal method.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:You can't.


John Vorwald wrote:You could use the ordinal method.



That depends on whether the OP is talking about an Enumeration or an Enum (enum). Given that he said "Enumeration" and is asking about the "previous element," my money is on Enumeration.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jayasri kurra wrote:How to get previous element in Enumaration.



Why do you want to? And why are you using Enumeration in the first place? If you tell us WhatNotHow(←click), (that is, what are you trying to accomplish--going backwards with an Enumeration is just how you're trying to do it) someone can probably advise a better approach to attain your actual goal.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's see

Enumeration: a predefined interface that tests for more elements and accesses the next element.

And the question is can Enumeration have previousElement();

Is it satisfactory to extend Enumeration? i.e.



Maybe something like


produces output
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Stuart A. Burkett wrote:You can't.


John Vorwald wrote:You could use the ordinal method.



That depends on whether the OP is talking about an Enumeration or an Enum (enum). Given that he said "Enumeration" and is asking about the "previous element," my money is on Enumeration.



Appears I need to read the question more carefully.... in order to avoid the dreaded "mouth in gear before brain is engaged" syndrome
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ignore. Seems I was talking rubbish.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:ListIterator class doesn't have hasPrevious or previous methods.



Not sure what ListIterator class you're talking about, but the java.util.ListIterator interface has

hasPrevious() and previous().
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Not sure what ListIterator class you're talking about


Neither do I Ignore that post.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now we just need to hear back from the OP as to what he's actually trying to accomplish and why getting the previous element in an Enumeration is a solution to that problem.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public Planet getPrevious() {
           return values()[(values().length + (ordinal() -1)) % values().length];
       }
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

That code appears to be the solution to a different question.
[edit]I see what has happened. You have interpreted the question as being about enumerated types, and used the values() array, whereas the original question was about the Enumeration interface, which I think is obsolete anyway.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic