代码之家  ›  专栏  ›  技术社区  ›  Maciaz

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

  •  0
  • Maciaz  · 技术社区  · 7 年前

    此处的示例代码:

    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;
        }
    }
    

    当项目[++顶部]出现时,会发生什么情况?

    2 回复  |  直到 7 年前
        1
  •  5
  •   Vel    7 年前

    这是预增量。等于:

    void push(char x) 
        {
            if (top == 99)
                System.out.println("Stack full");
            else {
                top = top + 1;
                items[top] = x;
            }
    
        }
    
        2
  •  2
  •   Youcef LAIDANI    7 年前

    这叫 预增量 ,所以这个 items[++top] = x; 相当于:

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