我正在尝试在控制器中设置一个模型,该模型在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;
}
}