Look into maps, though. See if you could put a matrix into a map using a string name as key, then pull it out again later.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Sep 05, 2005
Hi! Stan, Thanks for the idea of using Map, but I didnt quite get the funda..
To recapitulate my objective (which as of now is not allowed,directly that is ): 1. I want to get the user input of a string, say 'mat1' 2. Then declare a 2D integer array called mat1 (i.e.) int mat1 = new int ; 3. Then populate mat1 with integers (possibly again with user input.)
From, your thought provoking idea - all I could infer was to use the 'put' and 'get' methods of the Map interface so as to associate matrix elements' values and the string. But I couldnt proceed further.. and I dont know even if am on the right track of reasoning .. could you possibly throw some light on this?
Joined: Jan 29, 2003
Let's look first at how you would use dynamically named variables if they existed. Say this was valid syntax:
How would a later line of code ever reference this again? It would have to know the name. Maybe it could do
But I don't see how this is any better than using a real variable name for the array all along.
A map would let you store a bunch of arrays with user entered keys. We could do this in a loop for as many names as the user wants to enter:
Now you can reference these named arrays
If you want to see what names have been defined you can get the keySet() from the map and iterate through them.
Why do you want the end-user to be able to name your variables? the variable names are used by you (and the JVM) to keep track of stuff. the end user doesn't need control over what the JVM uses to refer to stuff...
i'm just asking to try to understand your problem better. it's like saying you want your bank to let you pick your bank account number... you don't really need control of that, it'll mess up the bank because there will be all kinds of different schemas, and what happens when two people enter the same number?
having said all that, Stan's suggestion seems pretty good. you create a Map that uses the user supplied input string as the key to find the array you want. Don't forget you'll still have to handle a user entering the same key-value more than once...
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Thanks Stan! ... for that nifty idea and explaining the actual mechanism to be used.
And Fred, I am not going to make use of it anywhere in actual coding but as you said Stan has explained the method in a lucid manner and it was Stan who tickled my curiosity about this hitherto unkown method.
Layne, I was'nt referring to "funda" of using maps but the "funda" of Stan's idea ... which I have understood now.