是否有人會介意澄清我到底能夠獲得一個for循環以遞增到我的代碼塊中的下一個對象?我知道外部正在遞增1,但是,像這樣的代碼在for循環中增加1,因爲在這種情況下,每個元素代表一個單獨的對象。建議將非常感謝!For循環的澄清
// create object
AccountWithInterface accountTemplate = new AccountWithInterface(name, balance, id, rate);
// declare array of 3 objects
AccountWithInterface[] accountArray = new AccountWithInterface[3];
// create each accountArray object
for(int i = 0; i < accountArray.length; i++) {
// copy each element (object) from accountTemplate
accountArray[i] = (AccountWithInterface)accountTemplate.clone();
// set balance for first object
accountArray[i].setBalance(85900.32);
// increment to next object/set object
accountArray[i].setBalance(3250.99);
System.out.println(accountArray[i].toString());
}
// My output currently keeps printing:
ID: 1122
Created: 04/13/14
Owner: Bob
Annual Rate: 4.50%
Balance: 3,250.99
ID: 1122
Created: 04/13/14
Owner: Bob
Annual Rate: 4.50%
Balance: 3,250.99
ID: 1122
Created: 04/13/14
Owner: Bob
Annual Rate: 4.50%
Balance: 3,250.99
實際上澄清如何增加下一個對象是我認爲我真的不瞭解這裏。 – user3434674
'accountArray'中有空值。 –
「增加到下一個對象」是什麼意思...?你正在遞增'i',因此通過訪問你在數組中移動的循環內的數組索引'[i]'。 'i'是0,然後是1,然後是2.那不是你想要的嗎? –