Java ternary operators and setting circular index values

I have a loop that traverses the for loop of ArrayList

If the condition is met in the for loop:

>I delete the current element from the ArrayList > reduce the size of the ArrayList local variable > reduce the index of the for loop like this, and check to make sure they never fall below zero

We just deleted the last element of ArrayList:

i = (i > 0) ? i-- : i;

My problem is that when I > the above will not decrease by 1 I've used ternary operators countless times, but I've never seen this behavior I've tested that I'm really > 0 and calling the I - part It just doesn't reduce I's value Delete the 0 value check and simply run I –; It did reduce me as expected

Editor 2: OK, someone edited my last edit. I mentioned that I don't use listiterator in this case, because the performance sensitivity of the loop itself is a key part of Android code

Solution

Me – reduces me, but returns the original value

I = I – decrements I and assigns it to its original value

You should use I – 1

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>