简体   繁体   中英

What is the correct way to do this?

I know this must be a fundamental design problem because I clearly can't do this. I want to call the ownGrokk, ownTyce, etc methods from another class depending on the value of the integer assigned to OwnedSpirits(int). This in turn fills arrays.

The problem is, I do this multiple times, and doing it from another class it seems like I have to make a new object every time to pass the new int argument, and doing so resets the value of spiritInstance. And, since that resets to zero, the arrays don't fill properly. I try to print out my array values later and I get an "ArrayIndexOutOfBoundsException".

public class OwnedSpirits {       
    private int spiritTypeInt = 0;

    public static int spiritInstance=0;                                                  
    public static int[] spiritarray = new int[9];
    public static String[] spiritName = new String[9];
    public static int[] party = new int[3];

    public OwnedSpirits(int spiritcall){
        if(spiritcall == 1){
            ownGrokk();
        }     
        if(spiritcall == 2){
            ownRisp();
        }
        if(spiritcall == 3){
            ownTyce();
        }
        if(spiritcall == 4){
            ownDaem();
        }
        if(spiritcall == 5){
            ownCeleste();
        }
    }

    private void ownGrokk(){ 
        spiritName[spiritInstance] = "Grokk";
        spiritInstance++;                
    }

    private void ownRisp(){     
        spiritName[spiritInstance] = "Risp";
        spiritInstance++;              
    }

    private void ownDaem(){ 
        spiritName[spiritInstance] = "Daem";
        spiritInstance++;      
    }

    private void ownCeleste(){ 
        spiritName[spiritInstance] = "Celeste";
        spiritInstance++;          
    }

    private void ownTyce(){ 
        spiritName[spiritInstance] = "Tyce";
        spiritInstance++; 
    }

and this code is in another class, where it attempts to call the methods to fill the array

buttonConfirm.addListener(new ClickListener(){
    @Override
    public void clicked(InputEvent event, float x, float y) {                                
        if(xcounter==3){
            for(x=0; x<3; x++){
                if(setdaemtrue == true){
                    new OwnedSpirits(4);
                }
                if(setrisptrue == true){
                    new OwnedSpirits(2);
                }
                if(setcelestetrue == true){
                    new OwnedSpirits(5);
                }
                if(settycetrue == true){
                    new OwnedSpirits(3);
                }
                if(setgrokktrue == true){
                    new OwnedSpirits(1);
                }    
            }
        }
    }
});   

and finally in yet another class:

System.arraycopy(OwnedSpirits.spiritName, 0, partylist, 0, 3);

@Override
public void show() {
    System.out.println(partylist[0]);
    System.out.println(partylist[1]);
    System.out.println(partylist[2]);

    spiritlist.setItems(partylist);
    table.add(spiritlist);
    table.setFillParent(true);
    stage.addActor(table);      
}

If the last part is confusing, it's because I am using libgdx. the print statements are there just to try to figure out why my list was having an error

I can show you what I would do to handle Spirits, and Parties. The Spirit class, contains name and current party its assigned to:

package com.stackoverflow.spirit;

public class Spirit {
    private String name;
    private Party party;
    private SpiritType type;
    private static int id = 0;

    public static enum SpiritType {
        Grokk, Risp, Tyce, Daem, Celeste
    };

    public Spirit(String name, SpiritType type) {
        create(name, type);
    }

    public Spirit(SpiritType type) {
        create(null, type);
    }

    // This is to handle Java inexistance of default parameter values.
    private void create(String name, SpiritType type)
    {
        Spirit.id++;

        this.name = (name == null) ? (type.name() + " " + id) : name;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public Party getParty() {
        return party;
    }

    public SpiritType getType() {
        return type;
    }

    /**
     * Used internally by @see Party
     * @param party the party this Spirit belongs
     */
    public void setParty(Party party) {
        this.party = party;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString()
    {
        return this.name;
    }
}

Finally the Party class, contains a set of Spirits, you can add and remove Spirits from the party.

package com.stackoverflow.spirit;

import java.util.HashSet;

public class Party {
    private HashSet<Spirit> spirits = new HashSet<Spirit>();
    private static int id = 0;
    private String name = "Party " + Party.id++;;

    public Party() {
    }

    public Party(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void add(Spirit spirit) {
        if (!spirits.contains(spirit)) {
            spirits.add(spirit);

            if (spirit.getParty() != null) {
                //Remove from previous party to update the other party set
                spirit.getParty().remove(spirit);
            }
            spirit.setParty(this);
        } else {
            // throw new SpiritAlreadyOnParty();
        }
    }


    public void remove(Spirit spirit)
    {
        if (spirits.contains(spirit))
        {
            spirit.setParty(null); // You could create a default empty party for "Nature/Neutral" Spirits perhaps :)
            spirits.remove(spirit);
        }
        else {
            //throw new SpiritNotInParty();
        }
    }

    public boolean isOnParty(Spirit spirit) {
        return spirits.contains(spirit);
    }

    public ArrayList<Spirit> getSpirits()
    {
        return new ArrayList<Spirit>(spirits);
    }

    public int getPartySize() {
        return spirits.size();
    }

    public String getPartyInfo()
    {
        StringBuilder builder = new StringBuilder();

        builder.append("Party:" + this.name + " Size:" + this.spirits.size() + "\n");
        for (Spirit s : spirits)
        {
            builder.append(s.getName() + "\n");
        }
        return builder.toString();
    }


    @Override
    public String toString()
    {
        return this.name;
    }
}

Here I use the Spirit and Party classes, you could add more functionality, like properties for party strength, magic buffs on the party, etc:

package com.stackoverflow.spirit;

import com.stackoverflow.spirit.Spirit.SpiritType;

public class Main {
    public static void main(String[] args) throws java.lang.Exception {
        Party griffindor = new Party("Griffindor"), slytherin = new Party(
                "Slytherin");

        // You can also do for (SpiritType type : SpiritType.values() then
        // type.ordinal()
        for (int i = 0; i < SpiritType.values().length; i++) {
            griffindor.add(new Spirit(SpiritType.values()[i]));
            slytherin.add(new Spirit(SpiritType.values()[i]));
        }

        Spirit mySpirit = new Spirit("NotAHPFan", SpiritType.Celeste);

        slytherin.add(mySpirit);

        System.out.println("Name of party:" + mySpirit.getParty().getName());
        System.out.println("Is on griffindor?:"
                + griffindor.isOnParty(mySpirit));

        // What now?
        griffindor.add(mySpirit);
        System.out.println("Is " + mySpirit.getName() + " on "
                + slytherin.getName() + "?:" + slytherin.isOnParty(mySpirit));

        System.out.println(mySpirit.getName() + " is now on "
                + mySpirit.getParty() + "\n");


        System.out.println(griffindor.getPartyInfo());
        System.out.println(slytherin.getPartyInfo());
    }
}

PD: I'm not a HP fan.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM