for loop - 测试计数器 - 为什么它的使用少于

时间:2013-08-18 15:37:41

标签: loops testing for-loop counter

我对for循环感到困惑,在“测试计数器”中为什么使用它(jerry< tom.length)...?

public static void main (String args[]) {

int tom[] = {1, 2, 3, 4, 5};    

    for (int jerry=0; jerry<tom.length; jerry++)
    {
System.out.println(jerry+ "\t" +tom[jerry]);

} }

1 个答案:

答案 0 :(得分:0)

java中的数组(c / c ++ / ruby​​等)从0开始索引数组而不是1 这就是为什么它以jerry = 0(第一个索引为0)开始并以数组的长度结束 - 1,这是最后一个索引(在您的示例中,最后一个索引为4)