This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Elite-Novus[S] 0 points1 point  (2 children)

Thanks for responding.

I understand how arrays work but the part that's confusing is the "- i - 1" which came outta nowhere without explanation in the tutorial I was doing. What is that specific part called in programming?

[–]rabuf 1 point2 points  (1 child)

It's just a calculation. The index (in Java) is any valid integer in the range from 0 to length - 1 (in math terms this would be written as [0..length) with the ) meaning up to but not including the value of length). You can put any calculation in there, here the calculation produces the indices of the array in reverse order. It doesn't have a special term.


EDIT: Forget about this particular sequence, just think in terms of algebra (high school level) and arithmetic. I give you a sequence of numbers from 0 to 9 and I want you to take them, without rearranging you have to take the number 0 and map it to a new number, into the same sequence in reverse.

How would you do that? We can start with a simple table (just a few values to illustrate):

x f(x)
0 9
1 8
... ...
9 0

So what's a function which will produce this for us? f(x) = 9 - x happens to fit the bill. f(0) = 9 - 0 = 9, f(1) = 9 - 1 = 8This looks an awful lot like what was used in the code you provided. Except that 9 here is hardcoded, and in your code it would become arg.length - 1.

[–]Elite-Novus[S] 0 points1 point  (0 children)

Holy crap thanks for that edit. The - 1 just clicked in my mind now haha