vixrant
vixrant

Reputation: 198

How can one pass array to a constructor in an enum?

I'm making an enum of Pokemon types. Each type has a strength, weakness, and immune Type. I'm trying to pass Types into different arrays which store the strength and weakness of each type.

I know that I can define an array as

int [] x = {1, 2, 3, 88};

And I've tried the same for passing an arrray:-

// For the sake of simplicity, all types have the same parameters for now...
    public enum Type {

    NORMAL ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    FIGHTING ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    FLYING ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    POISON ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    GROUND ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    ROCK ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    BUG ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    GHOST ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    STEEL ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    FIRE ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    WATER ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    GRASS ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    ELECTRIC ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    PHYSIC ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    ICE ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    DRAGON ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    DARK ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST}),
    FAIRY ( {Type.STEEL, Type.PHYSIC}, {Type.FIGHTING, Type.POISON}, {Type.GHOST});

    private Type [] weak, strong, ineffective;

    Type (Type [] weak, Type [] strong, Type [] ineffective) {

        this.weak = weak;
        this.strong = strong;
        this.ineffective = ineffective;

    }

}

My IDE, BlueJ, says "Illegal start of expression".

If not like so, how can I pass arrays into the constructor of enums?

Upvotes: 0

Views: 109

Answers (2)

David.Jones
David.Jones

Reputation: 1411

This was said by @Ruslan Ostafiychuk, but I wanted to clarify what he meant. Defining these sets as Type[] will fix your problem. To do this you need to change your code to:

// For the sake of simplicity, all types have the same parameters for now...
    public enum Type {

    NORMAL ( new Type[] {Type.STEEL, Type.PHYSIC}, new Type[] {Type.FIGHTING, Type.POISON}, new Type[] {Type.GHOST}),
    FIGHTING ( new Type[] {Type.STEEL, Type.PHYSIC}, new Type[] {Type.FIGHTING, Type.POISON}, new Type[] {Type.GHOST}),
    FLYING (new Type[]  {Type.STEEL, Type.PHYSIC}, new Type[] {Type.FIGHTING, Type.POISON}, new Type[] {Type.GHOST}),
    .
    .
    .
    FAIRY ( new Type[] {Type.STEEL, Type.PHYSIC}, new Type[] {Type.FIGHTING, Type.POISON}, new Type[] {Type.GHOST});

    private Type [] weak, strong, ineffective;

    Type (Type [] weak, Type [] strong, Type [] ineffective) {

        this.weak = weak;
        this.strong = strong;
        this.ineffective = ineffective;

    }

}

Also, to touch on the point of enumsets, it seems like using this could help you organize your development. I'm not that familiar with enumsets, but i'll take a stab at how you could use it. Something like:

EnumSet<Type> normal = EnumSet.of(Type.NORMAL.weak, type.NORMAL.strong, type.NORMAL.ineffective);

Upvotes: 1

Ruslan Ostafiichuk
Ruslan Ostafiichuk

Reputation: 4692

You have to use new

new Type[]{Type.STEEL, Type.PHYSIC}

Upvotes: 3

Related Questions