Java代码长度-1的含义 [英] Java code length -1 meaning

查看:60
本文介绍了Java代码长度-1的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Zillion {

    private int zilly[];

    public Zillion(int size){
        zilly = new int[size];
    }

    public void increment(){
        int i=zilly.length -1;
        while(i>=0){
            if(zilly[i]!=9){
                zilly[i]+=1;
                i=-1;
            }
            else{
                zilly[i]=0;
                i--;
            }
        }
    }

我只是得到了这样的基本Java代码,并且不知道长度-1在这部分(int i = zilly.length -1;)意味着什么.有人可以解释一下吗?

I just got a basic java code like this and do not know what length -1 means at this part (int i=zilly.length -1;). Can someone please explain this ?

推荐答案

数组的大小是该数组中元素的数量,但是数组的第一个索引是0.例如:

The size of array is the number of elements in this array, but The first index of arrays is 0. For example :

int zilly[] = {1, 2, 3, 4}

在此示例中:

zilly.size 返回:4

但是 zilly [4] 不存在,因为第一个元素的索引为0

But zilly[4] not exist because the index of the first element is 0

zilly.[zilly.size-1] 返回最后一个元素(4).

zilly.[zilly.size - 1] return the last element (4).

希望有帮助.

这篇关于Java代码长度-1的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆