数组[++变量]代表什么?

时间:2018-01-16 13:41:13

标签: java arrays iteration

示例代码:

static class stack 
{
    int top=-1;
    char items[] = new char[100];

    void push(char x) 
    {
        if (top == 99)
            System.out.println("Stack full");
        else
            items[++top] = x;
    }
}

当项目[++ top]出现时到底发生了什么?

2 个答案:

答案 0 :(得分:4)

这是预增量。它等于:

void push(char x) 
    {
        if (top == 99)
            System.out.println("Stack full");
        else {
            top = top + 1;
            items[top] = x;
        }

    }

答案 1 :(得分:2)

这称为预增量,因此此items[++top] = x;相当于:

top++; // this also equivalent to top+=1; or top = top + 1;
items[top] = x;