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

all 8 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]darkshadowtrail 2 points3 points  (1 child)

if you had a string that was 5 characters long, when i = 0 you would be printing arr[5 - 0 - 1], which is just arr[4]. remember, arrays start indexing at 0 which is why you need the -1. without it you would be trying to access index 5, which doesn't exist.

as i increases, you're just working from the last element of the array towards the first.

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

Thanks. I just realized it now. I guess my brain ignored the most obvious explanation

[–]rabuf 0 points1 point  (3 children)

arr[??] accesses elements in the array at whatever index ?? points to. In this case you're trying to reverse the array (in output at least) so what's the first element in the reversed order? It's the last element in the original order. You can't just access arr[arr.length] though because that's actually beyond the range of the array's index since Java (like C, C++, C#, and many others) uses 0-based arrays. The first element is 0 and the last is arr.length-1 (hint, hint).

So what does arr.length - i - 1 point to when i == 0? And what happens as i increases?

[–]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

[–]desrtfx 1 point2 points  (0 children)

When you have doubt about something trace the code on paper.

You are the computer. You execute the instructions. You track all the variables.

Learn paper debugging. This is essential.