Java array[i]++ vs ++array[i]

Java的数组[我] + VS C++数组[我]

问题 (Question)

I have an array of ints:

private int array[];

If I also have a method called add, then what's the difference between the following:

public void add(int value) {
   array[value]++; VS ++array[value];
}

p.s. on a separate note, what's the difference between int array[] vs int[] array ? Thanks

我有一个整形数组:

private int array[];

如果我也有一种称为添加,然后下面之间的区别是什么:

public void add(int value) {
   array[value]++; VS ++array[value];
}

注:在一个单独的说明,有什么区别和int数组[ ] [ ]阵列?感谢

最佳答案 (Best Answer)

what's the difference between int array[] vs int[] array ?

There is none. It's just java convetion to create arrays like int[] array, it's more clear.

If I also have a method called add, then what's the difference between the following:

   public void add(int value) {
       array[value]++; VS ++array[value];
    }

In this code, there is not any difference. But the difference in general is:

int x = 5, y = 5;

System.out.println(++x); // outputs 6
System.out.println(x); // outputs 6

System.out.println(y++); // outputs 5
System.out.println(y); // outputs 6

//EDIT

As Vince Emigh mentioned in comments below, this should be in answer too ...

As you know, ++ increments the number by 1. If you call it after the variable, your program will increment the number, and if needed imediately (like when you increment inside the println params), returns what the value was BEFORE incrementing (resulting in your 5). Adding it before your var will result in your program increasing the value right away, and returns the incremented value. If you dont use the variable imediately, like you do when youre printing it out, then it really doesnt matter, since they both increment.

有什么区别数组[ ] [ ]与int数组?

有没有。这只是Java公约创建数组一样int[] array它更清晰。

如果我也有一种称为添加,然后在下面有什么区别:

   public void add(int value) {
       array[value]++; VS ++array[value];
    }

在这段代码中,没有任何区别。但在一般的差异:

int x = 5, y = 5;

System.out.println(++x); // outputs 6
System.out.println(x); // outputs 6

System.out.println(y++); // outputs 5
System.out.println(y); // outputs 6

编辑

作为文斯Emigh在下面的评论中提到,这应该是在回答…

如你所知,++递增数量1。如果你叫它变量之后,你的程序将增加的数量,如果需要马上(比如你的增量内的输入参数),之前的价值是什么,收益递增(在你的5)。添加在你的变量会导致你的程序增加价值吧,并返回。如果你使用变量,就像你当你打印出来,然后并无关紧要,因为他们都增加。

答案 (Answer) 2

++ means the value will increment by 1 so if it's before the code it will increment before running that line if after well it will increment after

This 5 minutes video might help you understand https://www.youtube.com/watch?v=ydcTx6idTs0

+ +是指值将增大1,所以如果是之前的代码将运行该行之前,增量如果后会增加后

这5分钟的视频可以帮助你理解https://www.youtube.com/watch?V = ydctx6idts0

答案 (Answer) 3

This example will make pre -increment and post increment clearer In pre-increment the value is incremented before the operation , in post increment it is done before the operation.

        arr[0]=1;
        int var1=arr[0]++;
        System.out.println(var1);
        arr[0]=1;
        int var2=++arr[0];      
        System.out.println(var2);

Here is am doing an assignment, if I have a post increment , the increment occurs after the assignment. If i have a pre increment the increment occurs before the assignment

The output

1
2

这个例子将预先递增和后增量清晰预值增量递增,在手术前,后的增量是术前。

        arr[0]=1;
        int var1=arr[0]++;
        System.out.println(var1);
        arr[0]=1;
        int var2=++arr[0];      
        System.out.println(var2);

这是我做的一项任务,如果我有一个岗位增量,增量发生后的分配。如果我有一个预先增加之前发生的分配

输出

1
2

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/23451036