Forums Register Login

Help dissecting some Java code

+Pie Number of slices to send: Send
I am working through a tutorial to learn Java and I have come across this example.

public static void main(String[] args) {




I do not understand how the [i] is being used in this example. When I look at result[i] or any other variable with [i] at the end? I thought that the [i] means that you passed that specific array into that local variable within the for loop but I do not understand how you pass the length of another array in the result[i] part of the code?
results[i] = leftVals[i] + rightVals[i];
so would that mean when it runs through the first time that it is actually saying this.
0 = 100.d + 50.d;
This is very confusing to me. Than you for the help.



+Pie Number of slices to send: Send
"i" in this example and others where it see it used like this, "i" is being used as a loop variable for the "for" statement and as an index in the arrays.

What the loop says is for i beginning at 0 loop through the scope "{}" opCodes.length times, and where you see "i++" that is saying increment the loop variable by 1 each time through the loop.

When you see the square brackets [] that denotes an array, and the number or variable inside is the index used. In this case the variable "i" is being used, so each time you see the variable "i" in the code, you substitute the number of loop you are on. So first time through the scope, "i" is 0, second time "i" is 1, and etc.


Here is a link to the Java Really Big Index, I find it good for a reference or there are tutorials there too.
+Pie Number of slices to send: Send
Welcome to the Ranch.

Joe Simpson wrote:I do not understand how the [i] is being used in this example.


In the for loop body you initialize i to 0. As you know, first array element starts at index position 0 too. So what happens here.. When loop iterates, i gets different value every time loop does iteration.
So, first time you accessing an array opCodes[0], second time opCodes[1]... and like that up to while i less than op.Codes.length, which is 4 iterations in total, where i has values (0, 1, 2, 3). During the last iteration you accessing opCodes[3], which is the last element within an array.
Exactly the same happens with result[i], leftVals[i] and rightVals[i].

Please explain what result suppose to be when code gets executed?
1
+Pie Number of slices to send: Send
Please tell us which tutorial that was, so we can avoid it
That is horrible code. The opcodes do not have an obvious meaning; they might mean add subtract multiply divide, but they are in the wrong order. Why are the opcodes in an array in the first place? That tutorial uses parallel arrays, which suggests to me that they do not understand object‑orientation.
It is unnecessary to add a d to numbers which already contain a decimal point. 12345d is equivalent to 12345.0 and the d means to read the number as a double rather than as an int. But those numbers already have xyz.0 in, so they do not need a d.
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 542 times.
Similar Threads
Bytecode contains zero's and one's?
Arranging members of array in a ascending order?
Driver Exam - returning arrays and int vs int[] conversion issue
Noob Problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:05:38.