1

Good Afternoon, i would like to ask regarding the methods to use string as reference.

Let say, that i got two object of type LinkedList named ChinaShip and HongkongShip

Normally, if i wanted to access a method (for example getFirst()) i will type ChinaShip.getFirst()

Now, let say that in other object, i got a variable Destination which content is a String. The example of the Content will be China and Hongkong

Is it possible to use the content of the variable as the name for accessing the LinkedList object?

my approach first would be concatenate the variable first, which will be Destination + "Ship"

This will produce a string which is ChinaShip and HongkongShip

The reason i'm doing this way rather than comparing the string is that the Destination consist of hundreds of posibilities.

Thank You Very Much.

Regards,

3
  • 1
    No, but you could use a Map of some kind Commented Apr 1, 2014 at 8:37
  • u may try HashMap or LinkedHashMap where u can store info in key=value pair Commented Apr 1, 2014 at 8:45
  • looks like it will do. Thanks :D Commented Apr 2, 2014 at 8:46

4 Answers 4

1

Unfortunately you can't do that in Java. But this is closer with that:

HashMap<String, LinkedList> dest = new HashMap<String, LinkedList>();
dest.put("China", ChinaShip);
dest.put("Hongkong", HongkongShip);
.....

if(dest.containsKey(Destination){
    dest.get(Destination).getFirst();
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactyly what i need to do. Thanks Sire :D
1

You can use the reflection API. If the lists are declared in the class MyClass you can use the following code:

LinkedList list = (LinkedList) MyClass.class.getDeclaredField(Destination + "Ship").get(this);

This assumes that the above code is called from within a MyClass object, otherwise the get(this) call must be changed to get(myClassInstance). Though as MadProgrammer mentions, you might be better of using a Map.

Comments

1

You can use a Map and use the desitnation string as your key because these will be unique:

Map<Desitnation, value> destinations = new HashMap<Desitnation, value>();

then search through the Map for your destination key:

http://www.tutorialspoint.com/java/util/hashmap_get.htm

Comments

0

I think you need to re-work your architecture. You shouldn't have to create method names from Strings.

It seems to me like you need a Ship object which has a destination, and a linkedlist of Ships.

If you need to map ships by destination, what you can also do is have a Map where they key is the destination string, and the value is the linked list of ships going to that destination.

So if you do a map.get("China") you will get a list of ships going to China, and from there you can do what you want.

1 Comment

Yeah thats exactly what i need to do. Thanks a lot sir, i'll try doing that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.