2

I got an enum which is filled with "items" and I want to generate one random item of the enum and put it into my inventory, which I made out of an array. The array only has room for 10 ints.

public class InventoryEnums {
    public static void main (String[] args){

    int[] inventar = new int[9] // int array mit 10 Plätzen

    enum items {
        KARTE, MONSTERBALL, MONSTERDEX, FAHRRAD, VM03, HYPERHEILER, AMRENABEERE, TOPGENESUNG, ANGEL, TOPSCHUTZ
    }
9
  • 1
    Do you want to fill the array with random elements of the enum, or do you just want one random element? You can get an array of all elements of the enum by using items.values(). Note: int[9] has room for only 9 int's! Commented May 18, 2022 at 14:54
  • Thank you for your comment. Yes I first want to generate a random element from the enum and then put it into the array Commented May 18, 2022 at 15:00
  • If you only want one item randomly selected, why an array? Commented May 18, 2022 at 15:00
  • Enum should be named in the singular, and should start with an uppercase letter. Commented May 18, 2022 at 15:01
  • 1
    How do you get to the idea that new int[9] will generate an array with ten elements? When you pass a size of nine, you get an array of nine. It’s as simple as that. And when you want to store elements of type Items, you need an Items[] array, not an int[] array. Commented May 24, 2022 at 8:13

4 Answers 4

3

An enum should be named in the singular, and should start with an uppercase letter.

enum Item { KARTE, MONSTERBALL, MONSTERDEX, FAHRRAD, VM03, HYPERHEILER, AMRENABEERE, TOPGENESUNG, ANGEL, TOPSCHUTZ }

And I suggest you work at devising a more descriptive name than “Item”.

Get an array of all the enum objects by calling values.

Item[] allItems = Item.values() ;

Generate a random number. Use that number as the index into your array of all items.

int randomIndex = ThreadLocalRandom.current().nextInt( 0 , allItems.length ) ;  // ( inclusive , exclusive )
Item rando = allItems[ randomIndex ] ;

Or perhaps you meant to randomly sort (shuffle) all the elements of the enum. If so, make a List backed by the array. Call Collections.shuffle. Changes made to the list also affect the backing array. So the array is shuffled.

Item[] allItems = Item.values() ;
List< Item > list = Arrays.asList( allItems );
Collections.shuffle( list );

More briefly:

Item[] allItems = Item.values() ;
Collections.shuffle( Arrays.asList( allItems ) );

See this code run live at Ideone.com.

[MONSTERBALL, TOPSCHUTZ, KARTE, FAHRRAD, VM03, MONSTERDEX, ANGEL, AMRENABEERE, TOPGENESUNG, HYPERHEILER]

Sign up to request clarification or add additional context in comments.

1 Comment

You can use just nextInt(allItems.length) when the lower bound is zero.
2

You can get a random integer value that is in the range 0 to array.length as

int rand = new Random().nextInt(items.values().length);

Then you can get the enum for the random value generated using

items.values()[rand];

You then use this in a loop to fill the array.

1 Comment

oh just realized you already posted similar solution like mine ! upvoting as this is another good approach. :-)
0

Isn't this what you want?

public class App {
    public static void main(String[] args) {
        Random random = new Random();

        enum items {
            KARTE, MONSTERBALL, MONSTERDEX, FAHRRAD, VM03, HYPERHEILER, AMRENABEERE, TOPGENESUNG, ANGEL, TOPSCHUTZ
        }

        items[] inventar = items.values();

        int index = random.nextInt(inventar.length);

        System.out.println(inventar[index]);

    }
}

Comments

0

There is another way:

  enum Item {
        KARTE, MONSTERBALL, MONSTERDEX, FAHRRAD, VM03, HYPERHEILER, AMRENABEERE, 
     TOPGENESUNG, ANGEL, TOPSCHUTZ
   }
   Item[] inventar = Item.values();
   Item singleItem = inventar[new Random().nextInt(inventar.length)];
  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.