Results 1 to 4 of 4
  1. #1
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Default String named variables

    How do you go about initializing an object variable´s name with a string?

    Like I want to initialize a variable like this:

    Java Code:
    Obj ["variable_"+count] = new Obj();
    I want to do this because i want to access it from an array with its string name instead of numbers. Like id call the element from the array like this:

    Java Code:
    Obj retrievedObj = array["variable_100"];

    ?????

  2. #2
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Default

    The reason why I need it to work like this is because im working with the A* Algorithm for pathfinding and in one of the scenarios i have to check if some node has already exists and thus inspected and if i do this:

    Java Code:
    if(array["variable_"+INTVAR] != null){ //then i have inspected it aready
    }
    If there is some other fast way to look for a certain element in an array, that would be helpful...

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

    Default

    You can't and what's more, you don't want to. If you want to associate an object with a String, then consider using a Map such as a HashMap. If you want to associate an object with a numeric index of contiguous numbers such as a counter, then just use a simple array, or for more flexibility, and ArrayList.

  4. #4
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

Similar Threads

  1. parse string into 2 double variables
    By woodsie2523 in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 02:46 PM
  2. Displaying variables in a string?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 09-21-2009, 04:10 AM
  3. Named Variables and Exponents
    By kathekas in forum New To Java
    Replies: 5
    Last Post: 09-14-2009, 07:58 PM
  4. Pls HeLp Me (Design a class named Fan)
    By faw in forum Advanced Java
    Replies: 5
    Last Post: 04-21-2008, 08:25 PM
  5. named pipes
    By samson in forum Networking
    Replies: 1
    Last Post: 04-04-2007, 07:40 AM

Tags for this Thread

Posting Permissions

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