+ 2
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
4 Answers
+ 11
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
}
+ 9
This generic tutorial might be helpful for you ;)
If you have any further questions, just ask.
https://docs.oracle.com/javase/tutorial/java/generics/index.html
+ 1
oh awesome thank you!!
0
Oh i don't have any errors I'm just not sure how you randomly pick a class from a set of classes