gpt4 book ai didi

Java 数组[i]++ 与++数组[i]

转载 作者:行者123 更新时间:2023-12-02 08:56:20 31 4
gpt4 key购买 nike

我有一个整数数组:

private int array[];

如果我还有一个名为 add 的方法,那么以下有什么区别:

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

附:另请注意, int array[] 与 int[] array 之间有什么区别?谢谢

最佳答案

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

没有。只是java约定创建像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];
}

在这段代码中,没有任何区别。但总体区别是:

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

//编辑

Vince Emigh在下面的评论中提到,这也应该在答案中......

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.

关于Java 数组[i]++ 与++数组[i],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23451036/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com