Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Norm

Thread: Dynamic variable names

  1. #1
    reuben2011 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default Dynamic variable names

    Let's say I have a method that accepts a certain amount of parameters of the same type, let's say 4. However, I want to do things to those 4 objects dynamically. I have a few possible solutions, none of which I know how to implement. Here is the pseudo-java code for them.

    (1) This solution involves using dynamic variable names to access the objects from the method's parameter list. However, I don't know the actual syntax, implementation for dynamic variable names.

    public static void doStuff(Object obj0, Object obj1, Object obj2, Object obj3)
    {
    for(int i = 0; i < 4; i++)
    {
    ["obj" + i].doSomething();
    }
    }

    (2) This solution involves only accepting an array of size four. However, I don't know if you can explicitly control the size of the array that can be passed into a method in Java.

    public static void doStuff(Object[4] objects)
    {
    for(int i = 0; i < 4; i++)
    {
    objects[i].doSomething();
    }
    }

    (3) This solution involves getting the parameter names via a method's method that returns an array of parameters. Again, I don't know if there is such as array or not or some other implementation.

    public static void doStuff(Object obj0, Object obj1, Object obj2, Object obj3)
    {
    for(int i = 0; i < 4; i++)
    {
    doStuff.parameters[i].doSomething();
    }
    }

    Any help on one (or more) of these possible strategies would be very helpful. Or if anyone has any other ideas, that'd be great too. Thanks!

  2. #2
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    im not that a java crack , but i think the easiest one would be #2
    you could run the method from another class like this (if you have 4 Objects, that are not in a list):
    Java Code:
    private class Cheese
    {
    private Ketchup ketchup = new Ketchup();
    private Object[] list = new Object[4];
    
    public void Cheese()
    {
         for(int x = 0; x < 4; x++)
         list[x] = new Object();
    
         ketchup.doStuff(list)
    }
    }
    if you have an unknown amount of Object i would use an ArrayList.
    the code would look like this: (i hope theres no bug in it :P )

    Java Code:
    private class Cheese
    {
    private Ketchup ketchup = new Ketchup();
    private ArrayList<Object> list = new ArrayList<Object>();
    
    public void Cheese(int amountOfObjects)
    {
         for(int x = 0; x < amountOfObjects; x++)
         list.add(new Object());
    
         ketchup.doStuff(list)
    }
    }
    Java Code:
    public static void doStuff(ArrayList objects)
    {
    
        for(int i = 0; i < objects.size(); i++)
        {
    
            objects.get(i).doSomething();
    
        }
    
    }
    Last edited by Coold0wn; 08-31-2011 at 02:25 PM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    (2) looks Ok and notice that it doesn't have to be 4 - you can get the length of the array using its length. One would never pass arrays of Object like that in polite company though, but might just be your example. In Java things have type.

    (1) looks like php. You can't do that in Java.

    I don't really understand (3).

    -----

    It might help if you descibed what you are trying to do rather than how you are trying to do it.

  4. #4
    reuben2011 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Coold0wn View Post
    im not that a java crack ...
    Thanks for the suggestions. However, this is actually for a homework assignment and it requires that it includes a method that accepts four objects as parameters. A array/list like that would work but if let's say I only have control of the method and nothing else outside the method, then the outside user could easily invoke my method and pass an array or list of 5, 9, 2, etc elements (which breaks the requirement for the assignment.)

    Quote Originally Posted by pbrockway2 View Post
    (2) looks Ok ...
    I know I can access the length of the array with length but I can only access the length of the array once it has been passed to the method, I can't restrict the length of the array being passed to the method. However, if my professor isn't a stickler about the requirements, I could let the user pass an array to the method and test the length of the array and then throw an exception if length != 4.

    -----------

    I guess I could hard code it like below. However, I'm quite saddened by the fact that if theoretically the method had 1000 parameters, the below method wouldn't be efficient (in terms of the length of time it takes to type) and I wouldn't be able to find another alternative solution.


    public static void doStuff(Object obj0, Object obj1, Object obj2, Object obj3)
    {
    obj0.doSomething();
    obj1.doSomething();
    obj2.doSomething();
    obj3.doSomething();
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    if theoretically the method had 1000 parameters
    Put them in an array, then you can have from 0 to as many as you want.
    pbrockway2 likes this.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The point is thwt you only need to know the length of the array (or size of the list etc) inside the method - ie after the array or list has been created and passed - where you use it in the for loop. Four arguments or a thousand in an array would be processed using exactly the same code.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    You might also want to look into varargs: Varargs
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    reuben2011 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    I think people are misunderstanding my question. I know that I can pass an array into a method and use a for loop to go through the whatever number of elements. However, what if I wanted my method to only accept a very specific number of a certain amount of objects/parameters. For example, if I made a method that took 5 fingers as a parameter and trimmed the nails of each.

    public void manicureForOneHand(Finger[] fingersOnOneHand)
    {
    for(int i = 0; i < fingersOnOneHand.length; i++)
    {
    fingersOnOneHand[i].trim();
    }
    }

    The problem with this implementation though is that someone could call this method with a Finger array of 1 finger, 6 fingers, and so on. I know that the for loop will trim all the nails but the thing is I only want my program to perform the service of nail trimming on complete sets of hands (5 fingers.)

    -----------

    Another alternative, may be to create a new class (Hand) to contain all those Fingers and then pass the entire Hand to the method to ensure I get all the fingers on one hand.

    public class Hand
    {
    public Finger[] fingers = new Finger[5];

    public Hand(Finger f1, Finger f2, Finger f3, Finger f4, Finger f5)
    {
    fingers[0] = f1;
    fingers[1] = f2;
    fingers[2] = f3;
    fingers[3] = f4;
    fingers[4] = f5;
    }

    }

    public class manicureShop
    {
    ...
    public void manicureForOneHand(Hand untrimmedHand)
    {
    for(int i = 0; i < untrimmedHand.fingers.length; i++)
    {
    untrimmedHand.fingers[i].trim();
    }
    }

    }

    I'm guessing this alternative is fine. Although I could have accepted five parameters of type Finger in the manicure method and then just use five statements to trim each of them separately. However, I guess the same Hand object can be reused so perhaps the design strategy shown above is pretty decent.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Ohh I see. What exactly are you trying to avoid by not just taking each argument separately? Especially if you have another method that operates on a single parameter, then you can just pass each argument in.

    Or you could check the size of the array and throw an Exception if it's not the correct size. Or you could take a single Object that contains the specific number of other Objects (I believe that's one of your ideas). Any of these ideas works fine.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 07:22 PM
  2. How to update with dynamic variable?
    By mr_anderson in forum JDBC
    Replies: 9
    Last Post: 08-09-2010, 09:40 AM
  3. Replies: 0
    Last Post: 04-12-2009, 04:06 AM
  4. [SOLVED] Can variable names be dynamically created?
    By CJSLMAN in forum New To Java
    Replies: 4
    Last Post: 01-03-2009, 02:06 AM
  5. building file and variable names from variables
    By madad2005 in forum New To Java
    Replies: 2
    Last Post: 07-18-2007, 05:47 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
  •