Java newbie question | SoloLearn: Learn to code for FREE!


Java newbie question

So i basically want to make a game and part of that requires me to be able to make a list of different classes and randomly pick one of those classes I'm fairly new to java I've done the tutorial a few months ago but this is my first actual coding attempt in java

10/1/2017 6:46:13 PM

Daniel Robertson

4 Answers

New Answer


Generics provide a way to store objects of different classes in one collection, thought this might be useful for you. As for the random choice of an element of a collection: Use Math.random() to get a random double from 0 to length of the collection-1. Then cast it to int and take the element with that index, example: int index; // use Math.random() as described for (Object o: yourSet) { if (o.equals(index)) // do whatever you like }


This generic tutorial might be helpful for you ;) If you have any further questions, just ask.


oh awesome thank you!!


Oh i don't have any errors I'm just not sure how you randomly pick a class from a set of classes