-1
private State USA = new State("United States of America");
private State CAN = new State("Canada");
private State MEX = new State("Mexico");
private State[] stateArray;
public static void main()
{
}


public void addState(State state) //I need a way to add the Private objects called State into an array here. The command must take User Interface
{
    stateArray = stateArray.add(state);
}

好吧,总而言之,我需要的只是某种可以将项目添加到数组中的方法,如 addState 数组中所示

4

2 回答 2

1

使用List<State> sates = new ArrayList<State>();

public void addState(State state){
  sates.add(state);
}
于 2013-04-14T03:04:15.413 回答
0

如果要使用数组:

private State USA = new State("United States of America");
    private State CAN = new State("Canada");
    private State MEX = new State("Mexico");
    private State[] stateArray = new State[3];
    int index = 0;
    public static void main()
    {
    }


    public void addState(State state) //I need a way to add the Private objects called State into an array here. The command must take User Interface
    {
       if(index < stateArray.length)
           stateArray[index++] = state;
    }

或者您可以使用ArrayList数组代替。

private State USA = new State("United States of America");
    private State CAN = new State("Canada");
    private State MEX = new State("Mexico");
    private ArrayList<State> stateArray = new ArrayList<State>;
    public static void main()
    {
    }


    public void addState(State state) //I need a way to add the Private objects called State into an array here. The command must take User Interface
    {
        stateArray.add(state);
    }
于 2013-04-14T03:02:59.503 回答