+ 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

1st Oct 2017, 6:46 PM
Daniel Robertson
Daniel Robertson - avatar
4 Respostas
+ 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 }
1st Oct 2017, 7:31 PM
Tashi N
Tashi N - avatar
+ 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
1st Oct 2017, 7:18 PM
Tashi N
Tashi N - avatar
+ 1
oh awesome thank you!!
1st Oct 2017, 7:33 PM
Daniel Robertson
Daniel Robertson - avatar
0
Oh i don't have any errors I'm just not sure how you randomly pick a class from a set of classes
1st Oct 2017, 7:20 PM
Daniel Robertson
Daniel Robertson - avatar