• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java question  RSS feed

 
Dave Hyer
Greenhorn
Posts: 12
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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!
Dave
 
Dave Hyer
Greenhorn
Posts: 12
Eclipse IDE Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Bear,

Thanks for going easy on me.

I will make sure I do that the next time I post code.

SOOOO much easier to read
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
     
    Dave Hyer
    Greenhorn
    Posts: 12
    Eclipse IDE Linux VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Ashley,

    Thanks for the pointer where I messed up the code. The more eyes on something, the better imho.

    I like your idea of drawing it out.

    And your analogy regarding the street and houses.

    As I have not actually done that exercise, I want to see if your method will help me answer it.

    I do have issues with the Pool exercises, so that may just be me. The magnets ones, I just copy the page and cut out the snippets and play with them till I can make the code work.

    Thanks again!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!