Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a string as a reference in Java?

Tags:

java

I don't think i have the terminology correct, haven't been one for that. What i'm trying to do is get a string back , then use it to run functions. .. Example :

int slotNumber = ((j*3)+i+1);
String slotString = "slot"+slotNumber;

Regularly I can do this :

slot12.Draw();

And I want to be able to do this :

slotString.Draw();

With it substituting slotString with slot12 in a dynamic scenario. If i truly have to i could do something similar to :

if (slotString == slot1) slot1.Draw();
if (slotString == slot2) slot2.Draw();

And such, but i dont really want to use x number of lines for x number of slots.

Any help is appreciated :D

like image 684
Lutcikaur Avatar asked Aug 07 '26 09:08

Lutcikaur


1 Answers

A possible solution would be to use a HashMap where the key is the slotNumber and the value points to the slot. Then you could do something like the following.

//Initialize at the start of your program
HashMap<int, Slot> SlotHash = new HashMap<int, Slot>();

//Code to retrieve slot and call Draw().
Slot select = SlotHash.get(slotNumber);
select.Draw();
like image 166
Tyler Ferraro Avatar answered Aug 10 '26 00:08

Tyler Ferraro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!