sách gpt4 ai đã đi

Java Array[i]++ và ++Array[i]

In lại 作者:行者123 更新时间:2023-12-02 08:56:20 31 4
mua khóa gpt4 Nike

我有一个整数数组:

private int array[];

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

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

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

1 Câu trả lời

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

//编辑

giốngVince 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
Bài viết được đề xuất: delphi - 在 GLScene 中确定与视锥体的交集
Bài viết được đề xuất: version-control - 小商店,为什么选择 DVCS?
Bài viết được đề xuất: .net - 如何使用 MSBuild 从构建后事件运行目标
Bài viết được đề xuất: excel - 中断/中止 VBA 循环
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com