We've started using netbeans in our Java programming class, after using notepad++ for a while. While iterating through an array list. I used the following code:
for (int i=0; i<=randomarrayhere.length; i++)
Netbeans suggested to flip the position of i and array.length
for (int i=0; randomarrayhere.length>i; i++)
What do we gain by this?
Thanks!
The first would throw an ArrayIndexOutOfBoundsException
when i
reaches randomarrayhere.length
.
Aside from that (if you use i<randomarrayhere.length
), there is no difference.
you can either use randomarrayhere.length>i
or i<randomarrayhere.length
, but don't use randomarrayhere.length>=i
or i<=randomarrayhere.length
because if you call randomarrayhere[i]
anywhere in your forloop you will get an Exception since array indices are zero-based.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With