Java三元运算符和设置循环索引值
我有一个循环遍历 ArrayList 的 for 循环。
如果 for 循环中满足条件:
- 我从 ArrayList 中删除当前元素
- 减少 ArrayList 局部变量的大小 减少
- for 循环的索引,如下所示,检查确保它们永远不会低于零。
我们刚刚删除了 ArrayList 的最后一个元素的情况:
i = (i > 0) ? i-- : i;
我的问题是,当 i > 时,上面的方法不会将 i 减少 1。 0.我已经无数次使用三元运算符,但从未见过这种行为。我测试过 i 确实是 > 0 并且 i-- 部分正在被调用。它根本没有减少 i 的值。删除 0 值检查并简单地运行 i--;
确实会按预期减少 i 。
EDIT2:好吧,有人编辑了我的上一次编辑,我在其中提到我在这种情况下特别不使用 ListIterator,因为循环本身对性能敏感,位于 Android 代码的关键部分。
I have a for loop looping through an ArrayList
.
If a condition is met in the for loop:
- I remove the current element from the
ArrayList
- reduce the size of the
ArrayList
local variable - reduce the for loop's index like so, checking to ensure that they never go below zero.
A case where we have just removed the last element of the ArrayList
:
i = (i > 0) ? i-- : i;
My problem is that the above does not reduce i by 1 when i > 0. I've used ternary operators countless times, but never seen this behavior. I've tested that i is indeed > 0 and that the i-- section is being called. It simply isn't reducing the value of i. Removing the 0 value check and simply running i--;
does indeed reduce i as expected.
EDIT2: Ok, well someone edited out my last edit where I mentioned that I am specifically NOT using a ListIterator in this case due to the performance sensitive nature of the loop itself, being in a critical portion of Android code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
i--
递减i
,但返回原始值。i = i--
会递减i
,然后将其赋给其原始值。您应该使用
i - 1
。i--
decrementsi
, but returns the original value.i = i--
will decrementi
, then assign it to its original value.You should use
i - 1
.最小修复:
您可能会遇到这样的问题:
向后迭代:
另一个常见的解决方案是向后迭代,如下所示:
使用
ListIterator(除非对性能至关重要):
但是,使用
ListIterator
会更好:Smallest fix:
You may be after something like this:
Backward iteration instead:
Another common solution is to iterate backwards, like this:
Using
ListIterator
(unless highly performance critical):However, you're even better off with a
ListIterator
:你尝试过吗?:
通过预减,你应该解决所有问题。
Did you try?:
With pre-decrement you should solve all your problems.
像
i = i--
这样的赋值(对于i > 0
会发生这种情况)根本没有意义:您到底在这里期望什么?使用以下内容代替:或者更好:
Having an assignment such as
i = i--
(which happens fori > 0
) simply doesn’t make sense: what exactly are you expecting here? Use the following instead:Or, even better:
做正确的事情:使用迭代器循环数组列表并在迭代时安全地删除项目。
Do the right thing: use an iterator to loop over your array list and safely remove items while iterating.
您所描述的算法对于您尝试执行的操作来说是一个糟糕的策略:从 ArrayList 中删除元素是一个 O(n) 操作,因此在一般情况下将其应用于整个 ArrayList 是 O(n^2) 。
更好的策略:
这是 O(n),并且实际上会是更简单的代码(您不必担心摆弄循环边界等)
The algorithm you have described is a bad strategy for what you are trying to do: removing an element from an ArrayList is an O(n) operation so applying this to the entire ArrayList is O(n^2) in the general case.
A much better strategy:
This is O(n), and will actually be simpler code (you don't have to worry about fiddling with the loop bounds etc.)
考虑不要将三元运算符与 noop 一起使用。
如果(i>0)
{
- 我
}
准确描述了您想要的功能(基于您的示例)。
Consider not using a ternary operator with a noop.
if (i > 0)
{
--i
}
exactly describes the functionality you want (based on your example).
正如其他人所说,
i--
递减i
并返回原始值,这意味着您的代码将执行递减,但随后立即将其设置回原始值。您可以将其更改为
--i
,在这种情况下,将返回递减的值,但无论哪种方式,您都在使用--
进行不必要的工作,因为它必须设置i
,返回新值,然后再次将i
设置为新值。你最好只做
i-1
,它不会两次设置i
的值:话虽如此,因为三元的 false 选项不会'不做任何事情,您可能最好使用简单的
if()
:它更容易阅读,无论
--
的方向如何,并且无论结果如何,都不进行任何不必要的处理。As others have said,
i--
decrementsi
and returns the original value, meaning that your code would have performed the decrement, but then immediately set it back to its original value.You could switch it around to be
--i
, in which case the decremented value will be returned, but either way you're doing un-necessary work with the--
, because it has to seti
, return the new value, and then seti
again to the new value.You'd be better off just doing
i-1
, which won't set the value ofi
twice:All that said, since the false option of the ternary doesn't do anything, you'd probably be better of just using a straightforward
if()
:It's easier to read, doesn't matter which way round the
--
goes, and doesn't do any unnecessary processing, whatever the result.