• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help dissecting some Java code  RSS feed

 
Joe Simpson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!