gpt4 book ai didi

java - 循环条件变量的最佳实践

转载 作者:行者123 更新时间:2023-12-02 06:13:52 29 4
gpt4 key购买 nike

当编写像下面这样的循环时,最好使用用于初始化数组大小的值:

int n = foo();
int[] arr = new int[n];

for (int i = 0; i < n; i++) {
...
}

或使用数组的 length 属性:

int n = foo();
int[] arr = new int[n];

for (int i = 0; i < arr.length; i++) {
...
}

这是一个偏好问题,还是其中之一有优势?

最佳答案

它是双向的,但我认为最好使用 .length 因为你不需要添加一个无用且占用一些内存的变量。

关于java - 循环条件变量的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55884094/

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