Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Create an array of object references?  RSS feed

 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create an array of object references of the class you created in Exercise 2, but don�t actually create objects to assign into the array.
Code from exercise 2


Here's what I thought is meant, but I think I am wrong.


[ July 23, 2007: Message edited by: James Hambrick ]
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ch4ex1 [] a = {new ch4ex1(),new ch4ex1(" test")};


well, that line sort of fulfills the assignment then goes on to violate it

the first half ch4ex1 [] a would start the declaration of the array well
BUT

the second half {new ch4ex1(), new ch4ex1(" test ")} causes it to be
instantiated with two elements but then goes on
to create two new objects and initializes the two
slots with them.

There is another way to finish that declaration that instantiates the array but does NOT initialize it. See if you can dig that up and that will answer your question. At least I think so....
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I though that when you did not use "new" it did not initalize it. but ch4ex1 c, that is not right either.
[ July 23, 2007: Message edited by: James Hambrick ]
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I searched and searched and could not find anything on it, so I started fooling around with it and came up with.


no it does not fire off the constructors, that was the whole point of the lab BTW.
 
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 think you're all reading too much into this. There's a much simpler way to do it. It involves using the numeral "2" -- if, in fact, you want there to be 2 elements in the array.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number 2???

ch4ex1[1] = c;
ch4ex1[2] = d;

???
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, remember it said "don�t actually create objects to assign into the array"? So don't assign anything to the array elements, like those two lines of code do. Remove them and you're just about done (except for actually declaring an array with two elements).
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String is also a object, right?
How do you create an array of Strings with a lenght of 2 but don't assign string content to the array? with object references is the same process.

Hope it helps!
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am NOT trying to be sarcastic.... please don't interpret it that way... I am trying to help to lead you to the answer.

Go back to whatever text you have chosen for your Java training. Find the place where they very first mention arrays. They typically cover declaration, instantiation, assignment to the elements, and THEN declaration with initialization. Read right through that area and you will find it. And "simplicity is key"!
[ July 23, 2007: Message edited by: Bob Ruth ]
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It said

Create an array of object references of the class you created in Exercise 2, but don�t actually create objects to assign into the array. When you run the program, notice whether the initialization messages from the constructor calls are printed.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Mods.... if I sin a great sin, slap me gently and let me know....)

That was the assignment question that you are working on... I meant go back into the chapter reading area on arrays. Here is a link to what I am talking about.....

http://java.about.com/od/beginningjava/l/aa_array.htm

when you get there scroll down to Java Array Initialization and read that section. The first examples are the one I want you to see most. Then they show an alternate form.... THAT is the one you are trying to use here.

The difference between those two forms is that the latter one example instantiates the objects and puts them into the array. That is what your assignment is telling you NOT to do. Read that first paragraph and look at the first three examples.... THOSE create the array without creating and plugging in the elements.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure how to create an array with the length of two without placing information it. My book says that you cannot set the size of an array in Java, it goes on to describe that the compiler basically decided how big to make it when data is entered.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What book are you using?

There are two ways to create an array, and I've never seen a book not list both. If there is a book that only talks about one way, many folks here would be interested in knowing what that is.

If you read the page that Bob Ruth linked to, it lays them all out. pay special attention to the "java array declaration" paragraph and the "Java array initialization" paragraph.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay so the Thinking in Java 3rd edition free ebook is wrong then. It says that I cannot create an array and specify the size of it. Said the compiler did that, kept from having an index out of bounds. I still don't get the idea of createing an array of object references. Doesn't seem like I did that.

say this at the beginning
"The compiler doesn�t allow you to tell it how big the array is. This brings us back to that issue of �references.�"

The he uses this example way down towards the bottom of the page
int[] a = new int[rand.nextInt(20)];



[ July 24, 2007: Message edited by: James Hambrick ]
[ July 24, 2007: Message edited by: James Hambrick ]
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OKay, James......thanks for that reference to the Thinking in Java book. NOW I see where you are coming from.

If I can speculate, I am going to call that an "enforced suggestion". I went to my copy and read the whole part that you are talking about and NOW I see why I am probably causing more confusion than anything else for you.

I have to think that the author left this out just to make it easy for you but, trust me, you can do it!

Object [] obj1; // declares a reference to an array of Objects
// I dont know how many as we have not yet
// allocated storage.

obj1 = new Object[20]; //NOW I have created an array in memory
// that can hold 20 references to objects
// of type object. By default they are
// all set to null . I have not created
// an object yet, well other than the array
// itself but all 20 elements of the array
// are null and NOT pointing to an Object.


Object [] obj2 = new Object[20];
// This form does the exact same thing in one line. The declaration
// and the allocation of the array storage of 20 Object references
// all set to null.

The above is the basic form for creating an empty array of objects of type Object.

It appears the Mr Eckels is strongly recommending the style of pre-allocating the Objects themselves and initializing the array with their references at compile time, allowing the compiler to allocate the storage and define the number of elements. This is a valid and very popular technique. But given that your initial array said to create the array without creating and initializing the references.... the above is the only way you have to do it.

Again, sorry for the confusion and frustration but I hope that you can look at this as an expansion of your knowledge on the subject.

Best o'luck
Bob
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Hambrick:
The number 2???

ch4ex1[1] = c;
ch4ex1[2] = d;

???


While others have tried (really hard) to explain to you as best as possible, your real source of confusion, I want to explain one point that is not your immediate source of confusion (but is likely to bite you very soon).

Arrays in Java are zero-based. In other words, the first element of an array ch4ex1 is ch4ex1[0]. If you have an array of N elements say MyArray, the Nth element of this array would be MyArray[N-1].

- Anand
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew you could do that in C/C++ but if the book said it then I thought I could not in Java. Heard this book was real good for beginners. I have the SCJP book coming to me in a few days so I can study for SCJP. Hard going from VB6 to Java lol.

BTW I knew that the arrays start at 0, but thanks for mentioning it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!