• Post Reply Bookmark Topic Watch Topic
  • New Topic

Auto-Naming of references with integers  RSS feed

 
Benny Shelac
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello community,
I am fairly new to programming languages and am currently working my way through the HFJ book. At present I'm working on a snake project (guess there's no need to explain the game Snake) for excercising a bit. I think i have a rather solid concept as well as a sufficient understanding of the language itself. However, there is one issue I still can't deal with.
With each turn that passes(the snake's head moves one field), the game creates a new instance of the 'tail' class with pre-determined x and y coordinates as well as a given lifespan. Then the new tail object is added to a collection(not yet decided which one) that contains every tail object and gets removed as soon as the lifespan expires.
What I want to do is to name the reference of each tail object with its xy-coordinates, such as 'myTail4-23'. Unfortunately I can't think of any solution to do that other than declining every possibility manually (could take quite a time with a 30 x 40 grid...).
So I would appreciate any clues on how to transform an integer's value to a string and afterwards combining these strings to a reference.
Thank you in advance and please excuse my not-that-proper english(and maybe my not-that-proper use of java terminology).

Thank you,
Benny
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't think of why you would want to do that, but perhaps a Map is what you seek.

P.S. Try to avoid ganging up multiple questions in one topic. It either means one will be ignored, or that the topic becomes a hot mess of overlapping subjects.
 
Benny Shelac
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with your p.s. since I would answer myself with 'google is your friend' while thinking about a bit.

And why would i need that... well, there must be a way to name each reference uniquely. How else would I be able to invoke the method of a specific instance?
Although I am indeed thinking of different approaches, I would really like to know how it could be realized to have object references that already contain information about the instances they are related with.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benny Shelac wrote:
And why would i need that... well, there must be a way to name each reference uniquely. How else would I be able to invoke the method of a specific instance?

Not following you in the least.

Variable names are set at compile time -- if you want to create references with run-time dynamic names, a Map is what you need.
 
Benny Shelac
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see... so maps are something i did not stumble across yet. Then I'll have to read myself a bit into this topic.
But, just to get your point: something like

int x = 7;
int y = 20;
Tail ("myTail" + x + "-" + y) = new Tail(x, y, z);
myTail7-20.doSomething();

(I know this syntax is wrong, but I think you know what I try to express)
Would not work at all? Or would it work at least when x and y are initialized already at compile time?

Thanks for the answers already,
Benny
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. Definitely barking up the wrong tree.
 
Benny Shelac
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thank you for taking the time to clarify.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map example:


This allows you to put "named" references into the Map.
 
Benny Shelac
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I'm starting to get the idea, this seems to be kind of what I was looking for. Just have to wait for access to a real computer to try out this HashMap.
Once again, thanks for your patience!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMaps are wonderful things, and I'm sure you will find many uses for them. However, I agree with those who wondered why you need this at all. It smells unnecessary.

Benny Shelac wrote:And why would i need that... well, there must be a way to name each reference uniquely. How else would I be able to invoke the method of a specific instance?

You're adding all these tail objects to a collection, right? You don't need to know what names the objects had before they got into the collection. You can still obtain individual objects from the collection, give then a new (local, temporary) name, and do something with them. For example:

The name "tail" is only meaningful within this for loop. It's the name we chose to refer to each instance as we look at it. It doesn't matter what other names this instance may have been referred to with previously - we can ignore that. The for loop will ensure that, for each instance in the list, when we look at it, it will be referenced with the name "tail". That's all we need.

Alternately, if you don't want to loop through everything, but maybe just do something with the first (or last) entry, you can do that:

Again, "firstTail" is a new name for your object. It doesn't matter what name or names it had previously. "firstTail" is all you need, within the braces in which it's declared.

For that matter, it's also possible to do things with list elements without ever giving them new names:

This does exactly the same thing the previous code fragment did.

Does that help? Is there still some reason you need to give each object its own name?
 
Benny Shelac
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mike, this looks like the kind of code I am probably going for eventually. However, I just wanted to know whether there is any possibility to give objects names that contain the value of certain variables. While this may not be a good solution in this specific case, maybe it could come in use some time later. Although, according to your reactions, it seems not that likely...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!