Quick question

Status
This thread has been locked.

felldownstairs

Gypsy Boy | Java Developer
Supreme
Feedback score
4
Posts
783
Reactions
144
Resources
0
Didn't really know where to put this, and its more of a question on java in general.
So im making a Families part of a plugin im working, and the families are in an enum since they are from a tv show. As families are related to each other, is there anyway i can add a part to this enum where it can have a list of their relatives. i tried something like
TEST({Family.TEST, Family.WH})
but i couldnt put an arraylist in an enum. any help is appreciated. thanks.
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

C0an

Le Big Skid
Premium
Feedback score
6
Posts
159
Reactions
86
Resources
0
So your trying to group families or what?
 

felldownstairs

Gypsy Boy | Java Developer
Supreme
Feedback score
4
Posts
783
Reactions
144
Resources
0
So your trying to group families or what?
Well say we have family a, b, c, d.
a is related to c and d. on the enum for A, i want a list of relations which would be c and d.
c is also related to a and d, so for c i want an list of relations in the enum formatting part, which would be a and d.
 

FireFlower

Feedback score
6
Posts
228
Reactions
48
Resources
0
If you're trying to do what I think you're doing, you want something like this.

Code:
public enum Families {
    Noobs(Pros),
    Pros(Noobs),

    Orcs(Elfs),
    Elfs(Orcs);

    public Families relatedFamily;

    Families(Families val){
                    relatedFamily = val;
    }
}

However that doesn't work because in order to initialize Noobs, you must first initialize Pros, and vice versa.
You can probably get around that with something like this, however their might be a better solution.

Code:
public enum Families {
    Noobs(2),
    Pros(1),

    Orcs(4),
    Elfs(3);

    private int relatedFamilyId;

    Families(int RelatedFamilyId){
        relatedFamilyId = RelatedFamilyId;
    }

    public Families getRelated(){
        return getFamilyByID(relatedFamilyId);
    }
    
    private static Families getFamilyByID(int id){
        
        switch (id){
            case 1:
                return Noobs;
            case 2:
                return Pros;
            case 3:
                return Orcs;
            case 4:
                return Elfs;
        }
        
        return null;
    }
}
 

felldownstairs

Gypsy Boy | Java Developer
Supreme
Feedback score
4
Posts
783
Reactions
144
Resources
0
If you're trying to do what I think you're doing, you want something like this.

Code:
public enum Families {
    Noobs(Pros),
    Pros(Noobs),

    Orcs(Elfs),
    Elfs(Orcs);

    public Families relatedFamily;

    Families(Families val){
                    relatedFamily = val;
    }
}

However that doesn't work because in order to initialize Noobs, you must first initialize Pros, and vice versa.
You can probably get around that with something like this, however their might be a better solution.

Code:
public enum Families {
    Noobs(2),
    Pros(1),

    Orcs(4),
    Elfs(3);

    private int relatedFamilyId;

    Families(int RelatedFamilyId){
        relatedFamilyId = RelatedFamilyId;
    }

    public Families getRelated(){
        return getFamilyByID(relatedFamilyId);
    }
   
    private static Families getFamilyByID(int id){
       
        switch (id){
            case 1:
                return Noobs;
            case 2:
                return Pros;
            case 3:
                return Orcs;
            case 4:
                return Elfs;
        }
       
        return null;
    }
}
Thanks, I was helped by another user who said to do something like Orcs(new Family[]{Family.ELFS, Family.NOOBS}
 
Status
This thread has been locked.
Top