Thanks again for the suggestion to get Head First Java regardless of the age. And thank goodness for discount book sellers right?
So on page 66, there is some code. I have pasted it here with questions/observations. I am having a hard time visualizing the actions in my head. So any help is great appreciated. I should mention I was not ever a CS student when I went to college, so this is all on my own. Please be gentle lol
I thought about print statements, having seen that professional, but not sure where to put them in. And is there a way to visually diagram this kind of thing easily? I am a visual/kinethetic learner so I type it in then it should come alive in my mind, that then cements what my hands are doing. Like learning to hit a ball, but with code
Thanks in advance!
"No matter where you go, well, there you are."
Dave, welcome to The Ranch! Everyone here is gentle so don't worry about that, and remember, we all had to start out once upon a time (although some much longer ago than others)! A friendly word of advice when posting snippets of code: it is much easier for everyone to read if you put it in code tags since it does some pretty formatting, including keeping indentation.
Dave Hyer wrote:This is where I am getting confused. I am seeing 2 arrays with the same name! Which one is for the reference variables and for the HeapQuiz objects?
The code you have typed won't compile because you are trying to reference a primitive in an array of type HeapQuiz. Luckily, I dug out my old copy of Head First Java, turned to page 66, and you have mis-typed. The line should read:
Think of the hq reference variable as a container with a load of compartments (in this case 5) for a whole bunch of other reference variables, with each being assigned its own compartment. You access these other reference variables by using the square brackets and specifying a number inside them. This number is the index of the array and is a reference to the object referred to by the specified compartment in the container. So when the code assigns a value as above, you are in effect saying:
Try thinking about it in the following way:
A new street has a bunch of new houses on it, but they haven't been given a number yet.
The numbering scheme is that each house will be numbered one higher than the last.
It is the job of one of the construction workers to go and put numbers on all of the doors.
You can view the array in a similar manner:
hq is the street.
hq[x] is the house.
hq[x].id = x is fixing the number to the door of house hq[x].
With regard to visualising, get a pen and paper, draw what is going on (just draw a big rectangle on the left side to represent the array, divide it into the required number of boxes, number them, draw individual boxes on the right and put id= whatever number in, then draw and erase/cross out lines between them as the references change when you work your way down the list of assignments). In fact, I think that's what the book suggests...
"Twenty years from now you will be more disappointed by the things you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover." - Mark Twain