• Post Reply Bookmark Topic Watch Topic
  • New Topic

An array question  RSS feed

 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I'm reading through the Head First book to teach myself Java. There's my disclaimer.

Ok, I think I understand arrays as the book explains them (and as a friend once told me). Java treats an array as an object, that holds values in an indexed manner so it's easily referenced? Now, an array of objects would only hold the references for the objects that were created using it? (Since it was said an array doesn't hold objects, which makes sense.)

If there was a reason to do this, wouldn't it end up using more memory? You then would have a reference to the array, and an index to each reference within the array?

So, just to make sure I'm understanding this as I think I do (Please correct me if I'm wrong!)




So now don't I have a reference for the array, which then gives me references for the 5 objects?

I'm not sure why it's bothering me, and it could be just me being slow.

Nate (Probably just being slow)
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have a total of 6 object references. One for the whole array and one for each element of the array. This is 5 more references for a total of 6. I'm assuming that when you create the objects that the references are added to the array and when you're done, you set those object references to null so that they can be garbage collected.

-- Kaydell
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks. So, I've created 6 objects and 6 references, rather than just typing out a little more and only creating the 5 I need? Basically at the moment I can't think of a reason why you'd put the references in an array, unless perhaps you were dealing with something easily referenced like the 50 states and their capitals. (One of the uses an array has in PHP).

So am I missing something vitally important regarding arrays and object references?

Nate
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you're missing anything except the fact that arrays (and more generally, Collections) have many uses and are extremely common. Imagine you are writing any kind of text-processing code. How would you represent the words in a sentence? Sentences in a paragraph? Paragraphs in a document? Since you don't know how many of any of these there will be when your program is compiled, but only while it is running, some form of Collection is your only choice. Other examples include the items in an e-commerce shopping cart, the monsters on-screen in a video game, the objects in a drawing, etc.

And you'll do yourself an enormous favor if you banish from your head that little voice that worries about "inefficiency" at this level, because, like most people's little efficiency voices, it's often horribly wrong. Writing out five variables as opposed to using a five-element array means you'll write five times the amount of code! With an indexed array, each element can be processed by the same code in a loop; but with five scalar variables, you have to write some separate code for each one. This will generally use many times more space than the tiny array object and its references that you're fretting about.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!