java 递增时如何让数组的索引“翻转”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6826826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How could I have the index of an array 'roll over' when incrementing?
提问by intagli
So I have an Array with a length of 4. When I increment it by 1 and the number gets bigger than the length of the array, I'd like it to rollover.
所以我有一个长度为 4 的数组。当我将它增加 1 并且数字变得大于数组的长度时,我希望它翻转。
For example:
例如:
current_index = 3;
current_index++;
//current_index is now 0 again
current_index = 3;
current_index += 2;
//current_index would be 1
current_index = 0;
current_index--;
//current_index would be 3
I'm currently solving it with if-else like this
我目前正在用这样的 if-else 解决它
if (current_index == textviewlist.length + 1)
current_index = 0;
else if (current_index == textviewlist.length + 2)
current_index = 1;
else if (current_index == -1)
current_index = 3;
But I feel like this isn't an appropriate solution, or "good" code.
但我觉得这不是一个合适的解决方案,或者“好”的代码。
Edit: I tried your suggestion, but apparently java behaves strangely with negative numbers. When I try
编辑:我尝试了您的建议,但显然 java 对负数的行为很奇怪。当我尝试
current_index = (current_index - 1) % textviewlist.length;
Java takes the index "0", decreases it by 1 ("-1") and then
Java 取索引“0”,将其减 1(“-1”),然后
-1 % 4 = -1
I expected it to be 3, see Wolfram Alpha: -1 mod 4But apparently the java % operator is not the same as the modulo operator?
我预计它是 3,请参阅Wolfram Alpha: -1 mod 4但显然 java % 运算符与模运算符不同?
Edit 2: I found a solution here: Best way to make Java's modulus behave like it should with negative numbers? - Stack Overflow
编辑 2:我在这里找到了一个解决方案:使 Java 的模数表现得像它应该使用负数的最佳方法?- 堆栈溢出
I can just do:
我只能这样做:
current_index -= 1;
current_index = (current_index % textviewlist.length + textviewlist.length) % textviewlist.length;
回答by Karoly Horvath
You can use the modulo operator.
您可以使用模运算符。
current_index = (current_index + n) % 4;
回答by Xion
Divide the incremented index modulo the array's length:
除以数组长度为模的增量索引:
current_index = (current_index + n) % textviewlist.length
回答by eugene_che
You can use mod as follows:
您可以按如下方式使用 mod:
current_index = (current_index + i) % 4.
回答by David Z
Just set it to itself modulo 4 - or rather, the length of the list - after each increment.
只需在每次增量后将其设置为模 4 - 或者更确切地说,列表的长度。
current_index += 2;
current_index %= textviewlist.length;
or combined:
或组合:
current_index = (current_index + 2) % textviewlist.length;
You could also do this:
你也可以这样做:
current_index += n;
while (current_index >= textviewlist.length) {
current_index -= textviewlist.length;
}
although I'd be surprised if that isn't slower than the modulo operation, especially since your list length is a power of 2.
尽管如果这不比模运算慢,我会感到惊讶,尤其是因为您的列表长度是 2 的幂。
Either way, it might be a good idea to encapsulate all this into an increment()
function:
无论哪种方式,将所有这些都封装到一个increment()
函数中可能是个好主意:
int increment(int old_index, int n) {
return (old_index + n) % textviewlist.length;
}
EDIT:ah, I didn't realize you were working in Java. (I think C's modulo operator mimics the mathematical definition on negative numbers) A slight improvement on the solution you found would be
编辑:啊,我没有意识到你在用 Java 工作。(我认为 C 的模运算符模仿了负数的数学定义)对您发现的解决方案稍有改进是
int increment(int old_index, int n) {
return (old_index + n + textviewlist.length) % textviewlist.length;
}