LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-30-2007, 10:00 AM   #1
qscomputing
Member
 
Registered: May 2005
Distribution: Mandriva 2006 Free
Posts: 67

Rep: Reputation: 15
Java Question - referencing variables by name as a string


Hi,

I'm coding up a little game in Java ME. I want to be able to do something like this (pseudocode):

level1Data=[...]
level2Data=[...]
level3Data=[...]

function getLevel(int foo) {
return level{foo}Data;
}

My thought was something like PHP's "variable variables" (eg. if $foo contains "level1Data" then $$foo == $level1Data), but I don't know whether they exist in Java.

My other thought was to use an array - but can I store all my level data arrays (which are of different lengths!) in a single array?

Thanks very much for any advice,
- QS.
 
Old 01-30-2007, 11:01 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Why not using arrays (levelData[foo]) ?
 
Old 01-30-2007, 05:01 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
You could use Java's reflection API for this, but I don't think most versions of J2ME have any sort of reflection support. However, jlliagre is right, you're really just looking for arrays. And, Java's reflection really isn't made for this purpose.
 
Old 01-31-2007, 04:49 AM   #4
qscomputing
Member
 
Registered: May 2005
Distribution: Mandriva 2006 Free
Posts: 67

Original Poster
Rep: Reputation: 15
OK, thanks. I thought arrays would probably turn out to be the right thing, but I'm not very familiar with Java and I'ver just finished writing some Perl code. :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
string with content of variables chloraldo Programming 4 08-18-2006 09:34 AM
java string question(explaination please) arunvk Programming 6 03-23-2006 05:02 PM
referencing a file - newbie question FastOwl MEPIS 4 07-05-2005 03:35 PM
how to divide two string variables user_linux Programming 3 06-20-2005 04:54 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration