• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to create multiple (sequential?) names for new objects created in a loop  RSS feed

 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I need to create multiple objects of the same class and would like to do so in a loop.  For example, in a for/next loop, saying "ObjectType object = new object()".  However, I also need to put the objects, once created, into an ArrayList.  If each object has the same name, adding each object to the List replaces the previously added object rather than appending it to the List.  So I need to create a unique name for each new object; something like object1, object2,....  but I can't figure out how to accomplish this.

This seems like it ought to be really easy, but I can't build Strings because then I get an "already defined" error msg when I try to use the String as in "ObjectType builtString = new object()".  I can't use the loop counter variable because object names can't be/start with digits.  Again, this seems like it ought to be easy.  What am I missing?

Thanks.
 
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code you've tried so far?
 
Al Davis
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the loop code:

        for (int i = 0; i < 5; i++) {
            Vamp vamp1 = new Vamp((int)(Math.random() * length), (int)(Math.random() * height));
            vamp1.getVamps().add(vamp1);
            System.out.println("vamps size is " + vamp1.getVamps().size());
        }

I've trashed the String builder code but I tried (using both String and StringBuilder) something along the lines of:
        For (loop with i counter)
            String X = "object" + i;  // I messed around w/ various methods - valueOf(i), toString(i), etc.  Nothing seemed to matter.
            Vamp X = new Vamp();
        }

The first code leads to an ArrayList of size 1, no matter how many times the loop iterates.  The String builder approach won't compile.

And, while I'm whining about being dumb, the "code" button in the reply editing window inserts HTML-like tags in the text, but doesn't modify the code as it purports to do.  What am I doing wrong with that?
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need a List of Vamp declared outside of the loop. Then each time around the loop create a new Vamp and add it to the list. If you need to make sure that no two Vamps have the same coordinate you'll have to add the logic for that.
 
Al Davis
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think I understand your answer.  At present my vamps list is declared and resides in another class; thus the call:

[code=java]vamp1.getVamps().add(vamp1);[/code]

vamp1 is the object used to access the add method in the class Vamp.

This isn't a big deal, actually.  There's no reason I can't simply write out 5 "new Vamp" statements, each giving a new name and new coordinates.  But 1) I'm trying to learn as much as I can and 2) I'm trying to be slick!
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Davis wrote:I don't think I understand your answer.  At present my vamps list is declared and resides in another class; thus the call:



vamp1 is the object used to access the add method in the class Vamp.

This isn't a big deal, actually.  There's no reason I can't simply write out 5 "new Vamp" statements, each giving a new name and new coordinates.  But 1) I'm trying to learn as much as I can and 2) I'm trying to be slick!



I don't understand what you're doing. You used "vamp1" twice, seemingly adding it to itself which makes no sense. Where's the "new" taking place?
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "getVamps()" static? If so then you need to access it in a static way by using the class name instead of the instance.

Can you post the code for Vamp?
 
Al Davis
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first line within the loop I created a new object - vamp1.  The next line adds vamp1 to the ArrayList vamps, which resides in the Vamp class.  It is correct that my intent is to use the object vamp1 to access the Vamps class and the ArrayList vamps, and then add itself (vamp1) to the array list.  None of the members in either class are static.  It seems like a screwy way to do it to me, too, but I don't know another way to get around the error "cannot access non-static code from static environment (something like that) and it's best friend, "code has private access in class Vamp".  I see those damn messages a lot; wish I knew a better way to avoid them.

Here's the code for Vamp:

 
Al Davis
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, the code creating vamp1 is in the dungeon class.  Here's that code:

 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you create a new Vamp, which has-a list of Vamps, and which adds itself to the List. The result is that no matter how many Vamps you create, each List in the Vamp will only ever contain one Vamp, which is itself.
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, regarding your posts with the code tags in square brackets:
Below the edit window are "Options" which have checkbox items. You need to uncheck the one that says "Disable BB Code in this message".
Then your code will be formatted correctly.
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:So, you create a new Vamp, which has-a list of Vamps, and which adds itself to the List. The result is that no matter how many Vamps you create, each List in the Vamp will only ever contain one Vamp, which is itself.

Additionally, the reference to "vamp1" only lives inside the loop so that each time around the loop, and when the loop ends, all instances of "vamp1" will be garbage collected, i.e. will disappear.
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You will need a List of Vamp declared outside of the loop. Then each time around the loop create a new Vamp and add it to the list. If you need to make sure that no two Vamps have the same coordinate you'll have to add the logic for that.

 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What part of your requirements imply that each Vamp has its own list of Vamps?
 
Al Davis
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no requirement for a list of vamps; it was my (arbitrary) choice to aggregate them all in an ArrayList.  I considered a hashMap but then getting the individual X and Y values would be a PITA.  I'm certainly open to other, better suggestions.

I still don't know what you mean by "You will need a List of Vamp declared outside of the loop".  Is creating the List in a separate class not sufficient?

You wrote "So, you create a new Vamp, which has-a list of Vamps, and which adds itself to the List. The result is that no matter how many Vamps you create, each List in the Vamp will only ever contain one Vamp, which is itself."  ...and thus my original question!
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is creating the List in a separate class not sufficient?

The problem is that you don't have -a- list, you have -a-bunch-of- lists.
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Is creating the List in a separate class not sufficient?

The problem is that you don't have -a- list, you have -a-bunch-of- lists.


Also, the "class" says that each instance of the class will get its own new list.
 
Al Davis
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because I have defined the ArrayList in the Vamp class, I need to use the call:



to add vamp1 to the vamps ArrayList.  Using your code, I would be adding 5 instances of Vamp V to the ArrayList, all with the same name.  Will that work?  Would using a LinkedList give me a better ability to keep track of the vamps?  Does that really matter since they're only created once and will be differentiable based on their X.Y values?  Is it the getVamps() method in my code which is screwing things up, or something else?

As you can see, I really struggle with the design aspects of Java in general, and the inheritability/interface/abstraction issues in particular.  The problems are related, but I'm not sure how.

Thanks for your help, and your patience.
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Davis wrote:Because I have defined the ArrayList in the Vamp class, I need to use the call:



to add vamp1 to the vamps ArrayList.  Using your code, I would be adding 5 instances of Vamp V to the ArrayList, all with the same name.  Will that work?  Would using a LinkedList give me a better ability to keep track of the vamps?  Does that really matter since they're only created once and will be differentiable based on their X.Y values?  Is it the getVamps() method in my code which is screwing things up, or something else?

As you can see, I really struggle with the design aspects of Java in general, and the inheritability/interface/abstraction issues in particular.  The problems are related, but I'm not sure how.

Thanks for your help, and your patience.

You haven't defined a member variable for Vamp called "name", so there is no concept of "name" for a Vamp. If you mean "vamp1" or my "v", those are variable names and not the name of the Vamp. Somebody needs to own the list of Vamps, a single list, not multiple lists. You could make the list a static member of Vamp, meaning, only one list will exist regardless of how many Vamp instances are created. Or, you could have your Dungeon instance own the list of Vamps. It's not readily appearent which is a better approach at this point. Either way the list would be created ouside of the loop.
 
Rancher
Posts: 3596
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Davis wrote:Because I have defined the ArrayList in the Vamp class, I need to use the call:




That, though, is the problem, as Carey is suggesting.

If you need a List<Vamp> then that List should not be a part of Vamp.
Think of it this way.  I have a car, but does my car have a collection of other cars?

The answer (ought to be) "no" (barring some very odd car).

Your List<Vamp> probably shouldn't be part of the Vamp class, but maybe part of your Dungeon (as the occupants or whatever).

Then your makeVamps() method will add the new Vamp to this List instead, in the loop.
 
Al Davis
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.  You've addressed a question I've had for a while now - Should Lists, objects, etc be created within the class they pertain to, or should they be created elsewhere?  I've been taking the wrong approach of putting those elements into the class when possible, just for some arbitrary sense that that's what I "should" do, and it's been the source of a lot of frustration.  Your analogy of a car of cars is helpful.  Thanks again.
 
Sheriff
Posts: 23644
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Davis wrote:Should Lists, objects, etc be created within the class they pertain to, or should they be created elsewhere?



This is a wrong question. Objects should be created by a component whose responsibility it is to create them. There's no yes-or-no answer to that, the answer just comes out of the design.

And anyway your problem so far seems to be where to declare an object, not where to create it. I hope you realize that those are two different things. It's often true that an object will be created by the same class in which it's declared, but it can just as well be created in a different class and passed into the class which declares it via a setter method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!