Results 1 to 8 of 8
  1. #1
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default [SOLVED] Append an incremental number to variable name

    Is it possible to append the value of a variable to the name of another variable?

    I want to create several arrays from one input file, but I want to do it all in one nested loop structure that increments a value and appends it to the word "array" and uses that to create a new array. How would I do the following:

    Java Code:
    int arrayNumber;
    int[][] array (want to append arrayNumber here)  = new int[x][x]
    Is this possible?
    Last edited by porchrat; 05-01-2009 at 07:26 PM.

  2. #2
    Gudradain is offline Member
    Join Date
    Nov 2008
    Posts
    44
    Rep Power
    0

    Default

    Not sure, but I think that you can't dinamically change the name of a variable. So you got to rethink your architecture.

    But why do you want to do that?

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    no sadly this is not possible(I've looked into ways for doing it). It just seems terribly inefficient to write

    JButton button1 = new JButton();
    JButton button2 = new JButton();
    ....
    JButton button99 = new JButton();

    I wish Sun would look into implementing this. Something like a
    String String.setVariableName("button"+i) = new String();
    would be nice.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    So use a multidimensional array. Arrays aren't limited to 2 dimensions, you know.

    db

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Variable names are next to meaningless when your code is run. If the JVM doesn't care about them, why should you? If you want to associated a variable with a String, I'd suggest that you use a Map. If you want to associate a number, then use arrays as Darryl suggests above.

  6. #6
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    So use a multidimensional array. Arrays aren't limited to 2 dimensions, you know.

    db
    Unfortunately I'm not allowed to, this has to do with an assignment for university and I have to use 2D arrays only.

    I've since realised that I don't really need to store the array at all, therefore there is no need to create a new array each time.

    I've just created a class that overwrites the array, runs the methods that need to run against the array, overwrites it again etc. etc. until it is finished processing all the arrays it needs to handle.

    That way I don't need to create a new array each time and hence I don't need to change the variable name.

    If I were allowed to I would've created a three-dimensional array int[][][] and incremented the final value by one each time I wanted to create a 'new' array, unfortunately I wasn't allowed to.
    Last edited by porchrat; 05-02-2009 at 06:26 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm glad you've got it figured out. This or any forum will probably be able to better help you in the future if you specify all of your requirements in the first post. Best of luck.

  8. #8
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I'm glad you've got it figured out. This or any forum will probably be able to better help you in the future if you specify all of your requirements in the first post. Best of luck.
    Actually I find this forum exceptionally helpful. I'm always grateful for any help I get, whether it is applicable to my particular problem or not, knowledge is knowledge and, being a beginner, I am eager to absorb as many different approaches to programming problems as I can.

    More often than not I find people here can answer my questions. Unfortunately I think the reason my post wasn't fully explained is that my knowledge is not sufficient yet to explain my problem fully.

    I hadn't considered 3D arrays when I first posted because I didn't know 3D arrays existed, but I had reached that point (and asked my lecturer) by the time the reply arrived. Hopefully as my knowledge and experience grows so will the accuracy of my posts.:D

    Thank you all for your suggestions, every response helps me to better understand java.
    Last edited by porchrat; 05-06-2009 at 04:34 PM.

Similar Threads

  1. append variables to a text file
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-02-2009, 10:17 AM
  2. Replies: 3
    Last Post: 11-21-2008, 07:43 AM
  3. Can we append more than 255 chars to <a href>??
    By freddieMaize in forum Advanced Java
    Replies: 22
    Last Post: 07-18-2008, 04:04 PM
  4. JLabel append?
    By Jononomous in forum New To Java
    Replies: 0
    Last Post: 04-07-2008, 07:41 PM
  5. Simple append question
    By Rageagainst20 in forum New To Java
    Replies: 0
    Last Post: 12-20-2007, 11:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •