Reputation: 9355
So I have three enums and I want to make an ArrayList out of them. Here's my code:
enum Outlook {
SUNNY, RAINY, CLOUDY
}
enum Temperature {
HOT, COLD, MILD
}
enum Humidity {
HIGH, NORMAL, LOW
}
And I want my ArrayList to look something like:
{Outlook, Temperature, Humidity}
Here's what I've tried:
ArrayList<Enum> attributes = new ArrayList<Enum>();
attributes.add(Temperature);
But already Eclipse tells me "Temperature cannot be resolved to a variable". Any idea how to achieve what I'm aiming for here?
Upvotes: 4
Views: 3730
Reputation: 86509
I want to loop through all of the classes (Outlook, Temp, Humidity) and, for each of them, calculate information gain. This is a function that makes use of each possible value of a given attribute (Outlook, Temp, Humidity).
One possibility would be to define a common interface implemented by all the attributes. This would allow your information-gain function to use the same interface regardless of attribute. And no reflection is required. For example:
interface IAttribute {
Enum<?>[] getValues();
String getName();
}
This interface applies to attribute classes as a whole, and not a particular attribute value. Here's one way to do that.
private static final class OutlookAttribute implements IAttribute {
enum Outlook { SUNNY, RAINY, CLOUDY }
@Override
public Outlook[] getValues() { return Outlook.values(); }
@Override
public String getName() { return "Outlook"; }
}
// And similarly for Temperature and Humidity
Now you can create a list of attributes to pass to your function.
List<IAttribute> attributes = new ArrayList<>();
attributes.add( new OutlookAttribute() );
attributes.add( new TemperatureAttribute() );
attributes.add( new HumidityAttribute() );
And your function definition can iterate through attributes.
for ( IAttribute attribute : attributes ) {
Object[] values = attribute.getValues();
...
for ( Object value : values ) {
...
}
...
}
Upvotes: 1
Reputation: 10797
I'm surmising that you want the names of the enums themselves, and not their values. As such, try the following code:
public static void main (String[] args) throws java.lang.Exception
{
List<Class<? extends Enum>> myEnums = new ArrayList<>();
myEnums.add(Outlook.class);
myEnums.add(Temperature.class);
myEnums.add(Humidity.class);
System.out.println(myEnums);
}
enum Outlook {
SUNNY, RAINY, CLOUDY
}
enum Temperature {
HOT, COLD, MILD
}
enum Humidity {
HIGH, NORMAL, LOW
}
You can see the demo here which will output:
[class Ideone$Outlook, class Ideone$Temperature, class Ideone$Humidity]
If you strictly need something like:
[Outlook, Temperature, Humidity]
and just can't like the extra class Ideone$...
info then we can add some extra sophistication.
Upvotes: 2
Reputation: 61
If you need the all values from a enum typ. Use Enumtyp.values().
public interface EnumInterface {
public static enum Outlook {
SUNNY, RAINY, CLOUDY
}
public static enum Temperature {
HOT, COLD, MILD
}
public static enum Humidity {
HIGH, NORMAL, LOW
}
}
public class EnumImp {
private static ArrayList<Enum> list = new ArrayList<>();
public void addUnits() {
EnumInterface.Outlook[] outlookValues = EnumInterface.Outlook.values();
EnumInterface.Temperature[] temperatureValues = EnumInterface.Temperature.values();
EnumInterface.Humidity[] humidityValues = EnumInterface.Humidity.values();
for (int i = 0; i < outlookValues.length; i++) {
list.add(outlookValues[i]);
}
for (int i = 0; i < temperatureValues.length; i++) {
list.add(temperatureValues[i]);
}
for (int i = 0; i < humidityValues.length; i++) {
list.add(humidityValues[i]);
}
}
public void output() {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
public class TestEnum {
public static void main(String[] args) {
EnumImp e = new EnumImp();
e.addUnits();
e.output();
}
}
Upvotes: 0
Reputation: 17142
You need to specify which values to add:
attributes.add(Temperature.HOT);
attributes.add(Temperature.COLD);
Temperature
all alone evaluates to a class
, but your ArrayList
can only contain Enum
s (Temperature.HOT
, Humidity.HIGH
...)
Upvotes: 0