如何将数组从我的Controller传递给模型,然后将其发回?

时间:2016-03-14 03:05:55

标签: java arrays

我正在尝试在控制器中设置一个模型,该模型在setArray中抓取,然后我反转顺序并使用getArray返回它,以便控制器可以告诉视图将其打印出来。

控制器

public class ReverseArrayController extends Controller {

    protected ReverseArray myModel;
    public ReverseArrayController( View theView, ReverseArray theModel){
        super(theView);
        myModel = theModel;
    }
    public void go() {


        myView.start();
        myModel.setArray int([12, 56, 34, 79, 26]);
        myModel.reverse();
        myView.say ("The Array Reversed is " + myModel.getArray());

        try {
            myView.stop();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我的模特

public class ReverseArray { 

    private int myArray[];

    public void setArray(int[] theArray){
        myArray = theArray;         
    }

    public void reverse(int[] myArray){ 
        for (int i = 0; i < myArray.length / 2; i++) {
            int temp = myArray[i]; // swap numbers 
            myArray[i] = myArray[myArray.length - 1 - i]; 
            myArray[myArray.length - 1 - i] = temp; }   
    }

    public int[] getArray(){
        return myArray;                     
    }   
}

0 个答案:

没有答案