使用变量访问多个对象

时间:2012-04-29 02:15:34

标签: java variables for-loop

我希望能够按如下方式访问for循环中的对象:

for (int i=0; i<5: i++)
{ object[i].doSomething(); }

然而,object[i]部分的语法让我感到厌烦。

1 个答案:

答案 0 :(得分:2)

for (        // loop
int i = 0;   // initialize the variable i before starting to iterate
i < 5;       // perform the block below while i < 5
i ++ )       // increment i after performing the block below
{                      // start of block to execute in each iteration of the for-loop
    object[i]          // the i-th element of the array object
    .doSomthing();     // call this method on ^^ 
}                      // end block

好的参考资料: