Local and Global variables
I am having trouble storing a node into an arrayList of nodes in two separate for loops (not nested). There is a big scoping issue. I need to read in a number of values in the first for loop and store values to this node, but then I have to use another for loop to read in a c开发者_JAVA技巧ompletely different value no accessable in the first and store it to the same node. I tried making this node global, but it does not seem to work. And I cannot initialize it before the for loop because the values needed to be associated with it are in two separate for loops like I mentioned which are not nested. There has to be a simple solution to this problem I am not seeing.
We don't need to see the code -- especially since it is incorrect -- but we do need to see a clear, unambiguous description of the problem. Otherwise if the problem isn't defined than any solution is meaningless since it might be the correct solution to a completely different problem.
Here's my guess at what you're doing:
- Define the array outside the scope of the 2 loops.
- Go through the first loop reading into the nodes (I'm guessing that you're initializing an array of nodes, and have to read the information in for each node and store it in the array.
- Go through the second loop making changes to the nodes that you defined in the first loop.
Now, clearly you must have some way of referencing the node for both loops i.e. a unique id or key, but this isn't clear. It would be helpful if you could describe your problem more clearly.
精彩评论