我的问题是我无法更新对象数组的值...
代码是:
public class GlobalVariable {
public int noOfSms = 0;
public CheckingClass object = new CheckingClass ();
public static void main(String[] args) {
GlobalVariable call = new GlobalVariable ();
call.driver();
}
private void driver() {
for(int i = 0 ; i < 3 ; i++){
object = CheckingFun();
// System.out.println("The No Of Sms"+noOfSms);
System.out.println("Array Value"+object.array[noOfSms] + " The number value"+object.number);
}
}
private CheckingClass CheckingFun() {
System.out.println("The No Of Sms "+noOfSms + "\n");
object.array[noOfSms] = noOfSms;
object.number = noOfSms;
noOfSms = noOfSms + 1;
return object;
}
另一类是:
public class CheckingClass {
public int number ;
public int[] array = new int [5];
}
这里的对象是另一个类的对象。我的问题在这一行
object.array[noOfSms] = noOfSms;
输出如下
The No Of Sms 0
Array Value0 The number value0
The No Of Sms 1
Array Value0 The number value1
The No Of Sms 2
Array Value0 The number value2
我不明白的是为什么数组的值没有更新。当作为对象的属性的 number 的值被更新时。
但我想要的输出是:
Array Value0 The number value0
The No Of Sms 1
Array Value1 The number value1
The No Of Sms 2
Array Value2 The number value2