2

Can anybody explain, why do I have to use this code pattern?

    // Create the array to store the CDs.
    CD[] cdLibrary = new CD[20];

    // Populate the CD library with CD objects.
    for (int i=0; i<20; i++)
    { cdLibrary[i] = new CD(); }

I cannot understand why the initialization of objects in an array does not occur when I call new CD[20]. It seems like I'm writing excess code. Can one of these steps be skipped?

User
  • 1,118
  • 7
  • 23
Kirill Golikov
  • 1,354
  • 1
  • 13
  • 27
  • You might want to consider using List instead of arrays: http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which – User Aug 21 '13 at 16:44

2 Answers2

8
CD[] cdLibrary = new CD[20];

This does nothing more than creating the 'container' where you will store the references. Consider it the cookie jar, fit for 20 cookies. At this point you haven't actually created a cookie yet, you have just created a jar (array) which can hold a specified amount of cookies (CD objects in this case).

for (int i=0; i<20; i++) {
 cdLibrary[i] = new CD(); 
}

This will create 20 cookies and put it inside the jar.

Edit: Jon Skeet mentions a very good point in the comment section, be sure to check it out. His analogy doesn't have this issue and should be considered more accurate (although cookies are more fun than paper).

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • 2
    I don't particularly like the cookie jar analogy, as cookie jars aren't naturally *ordered* - that's why I've given an analogy of a book with a certain number of pages. (It also works with the analogy I frequently use between a variable and a sheet of paper, but that's a slightly different matter.) And it's also important to distinguish between objects and references - the jar doesn't hold CD *objects*, it holds references. – Jon Skeet Aug 21 '13 at 16:28
  • @JonSkeet: I totally looked past the ordering. Your analogy is indeed more suitable then, I have referred to it in an edit. – Jeroen Vannevel Aug 21 '13 at 16:34
  • I'm distort by C++ - there new init too. What does it keep references for? Do I get ordered list? I do not think so. Or if it is problem of garbage collection, but why does initialization single out? – Kirill Golikov Aug 21 '13 at 16:47
  • You don't get an ordered list (that implies there is some criterium to order with) but rather the list maintains the order elements have been entered. A cookie jar has no way of holding an order (a `bag` collection would be appropriate here). There are no objects in the cookie jar, only references to the objects you create. There has been written a lot about this, better than I could. – Jeroen Vannevel Aug 21 '13 at 17:04
8

I cannot understand why the initialization of objects in an array does not execute in the operator new.

Do you mean this line?

CD[] cdLibrary = new CD[20];

That doesn't initialize 20 objects. It initializes the array, and only the array - the array has 20 elements, each of which has a value of null (a null reference) to start with1. It's like creating a book with a given number of empty pages; if you want the pages to contain information, you have to write on each one separately, which is what the later loop does.


1 I'm assuming that CD is a class type here, for simplicity.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I can guess it. But what for? why do not make it simple? because, when I correct code (I do not know it is in msdn), I decide that I make something wrong, writing code like this. Maybe it is problem of philosophy... – Kirill Golikov Aug 21 '13 at 16:51
  • 3
    @so-olitary: So you're suggesting that the language should *automatically* populate the array by calling the parameterless constructor for the class `n` times? What if there *is* no parameterless constructor? Or what if I want to actually populate it in a different way (e.g. using the same loop as before, but passing in `i` as an argument to the constructor on each iteration). In my experience, I *very rarely* want to just populate an array by calling the default constructor `n` times, and I'd hate it if the language made that assumption for me. – Jon Skeet Aug 21 '13 at 16:55
  • 1
    It's also simple enough to create your own method to create and initialize an array with objects using the default constructor, should this be an operation that you happen to perform often enough to justify it. – Servy Aug 21 '13 at 16:57