【发布时间】:2014-05-26 19:15:35
【问题描述】:
public Account [] search(double bal){
boolean flag=false;
Account [] tmp = new Account[account.length];
//Loop until the end of the account array.
for(int i=0;i<account.length && account[i]!=null;i++)
{
if(account[i].getBal()==bal)
{
tmp[i]=account[i];
System.out.println(tmp[i]); //here returns correct value
flag=true;
}
}
System.out.println(tmp[0]); //here returns null
return tmp;
}
嗨。我对 Java 很陌生。
System.out.println(tmp[i]) 返回正确的 Account obj 并打印值。
但是为什么 System.out.println(tmp[0]) 在 for 循环之外返回 null 呢?
【问题讨论】:
-
拿出笔和纸,运行你的程序,标记每个对象的值。
-
或者,使用调试器。