• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create objects in a loop  RSS feed

 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if I'm asking this the right way, but here goes:

Is it possible to use a loop to create objects? I'm trying to put together a program (just as a way of learning) to run a horse race.
What I'd like to is have the user enter the number of horses they want in the race, and then create the objects as the user enters the various attributes of the horse (at least the horse's name), and then once all the instance variables have been set, create the first horse object and then move on to the second one, etc, until all the horse objects have been created.

I know I can't use something like a for loop to do it, so how would the code be written? Any suggestions?



 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You CAN use a loop. The trick is that you have to save the reference to each one as you create it. A simple way would be to use an array. You have to declare the array outside the loop, then use your loop counter as the index into the array...something like (this is just pseudo-code):

 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Fred; a question if a may.

I thought bout using an array, but (and I may be getting ahead of myself) what if I didn't want to use an array? Would there be another way to do this?

This is purely an academic question (or perhaps mostly academic): would there in fact be 10 separate objects with this approach, or is it just one object with 10 elements in it? Or are the elements references to 10 separate objects? I ask because I'm trying to understand what an array is exactly.

Like I said, it's academic, but if it's just one object, then what would be done if I wanted 10 separate objects to create instead of one horse array object with 10 elements? Am a making any sense at all? or would it make any difference with either approach aside from any simplicity with one or the other?
 
Steve Alpine
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array is not an object, an array holds references to objects, therefore if you have an array with 10 different elements, you have 10 different references to 10 different objects.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

An array is not an object


Array is object. An array of array holds the reference of the array objects
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Alpine wrote:An array is not an object (...)

An array is an object.
This for example prints true:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote:I thought bout using an array, but (and I may be getting ahead of myself) what if I didn't want to use an array? Would there be another way to do this?

Yes. There are at least a dozen ready-made collections in the standard Java Collections Framework that comes with the SDK, including Collections themselves (java.util.Collection), Lists (java.util.List), Sets (java.util.Set), and Maps (java.util.Map), and dozens more out there in Internet-land.

However, in answer to your other question, "would there in fact be 10 separate objects with this approach", the answer is:yes. An array of 10 objects will always involve at least 11 objects (1 for each element, plus one for the array itself); and the same is true of collections. The main difference between arrays and collections is that arrays can hold primitives (int, char, etc.); collections can only hold objects.

HIH

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say there would be AT LEAST eleven objects. The Array (or whatever collection type you use) + the ten Horse objects. But since each Horse object could have a String name, Integer registrationNumber, or who knows how many other objects....there could be many more.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, non-primitive variables hold references. When you do



what you are doing is creating a new Horse object and putting a reference to that object inside the horse variable. Now, if you did this



The second line copies the reference.. not the object that it refers to. There is only one Horse object

Now.. and array is a special kind of Object. It's an object that holds multiple references. Again,, it holds references, not objects..

So, when you do this



You have basically created an array object that can hold references to 10 horses. The 10 horses don't exist yet. The array is empty. Also remember that the variable stable itself contains a reference to the array object. Everything is a reference.

Doing this



This creates an arrray object that can hold references to 10 horses and puts a reference to that array in the stable variable. Then it creates one horse and puts a reference into the horse1 variable. Then it copies the reference to the first element in the array. So, not, the array that stable referes to contains a reference to 1 horse and 9 other references are empty

You could shorten the above code to



The end result is the same. You are just taking a shortcut and putting the reference to the Horse object directly into the array object

Now, to fill all the 10 horsies in the stable.. you could do this



Or do this



Both are equivalent as far as how they execute. However, the second version is more flexible.. Because you can easily increase the size of the stable like this

>
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!