Passing variable name to method

Hello Everyone,

I'm sure it's an easy one, but my brains have overheated and I'm lost.

I'm making a class which has ArrayLists as fields. I have a method which should fill an ArrayList with initialized custom datatype objects. Instead of writing such a method for each of the field ArrayLists, I would like to pass the concerned ArrayList name to the method as a parameter, so the method knows which of the ArrayLists it has to work with.

How do I do that?

    class Something {

      ArrayList<CustomDatatype> things1;
      ArrayList<CustomDatatype> things2;
      ArrayList<CustomDatatype> things3;

      Something() {
        things1 = new ArrayList<CustomDatatype>();
        things2 = new ArrayList<CustomDatatype>();
        things3 = new ArrayList<CustomDatatype>();
      }

      public void fillList(int i_dont_know_how_to_do_that) {
        // Add objects to the specified ArrayList
        // How do I target one ArrayList with the method parameters?
      }
    }
Tagged:

Answers

  • Why don't you just pass the ArrayList itself?

    class Something {
    
      ArrayList<CustomDatatype> things1;
      ArrayList<CustomDatatype> things2;
      ArrayList<CustomDatatype> things3;
    
      Something() {
        things1 = new ArrayList<CustomDatatype>();
        things2 = new ArrayList<CustomDatatype>();
        things3 = new ArrayList<CustomDatatype>();
    
         fillList(things1);
         fillList(things2);
      }
    
      public void fillList(ArrayList<CustomDatatype> list) {
        list.add(...);
        list.add(...);
      }
    }
    
  • Oh. Yeah, pretty obvious. :\"> Thanks!

    But what if I want to invoke this method from outside the Something class? Can I use a syntax which would "dynamically formulate" the parameter? Here's a silly test program for the Something class:

    Something something;
    
    void setup() {
      something = new Something();
    }
    
    void draw() {
      int PICKED = int(random(3)+1);
      something.fillList(something.thingsPICKED);
    }
    

    I know it's stupid the way it's put, but what's the right way to do it?

  • edited August 2014

    There are some issues about your plea! 8-X

    • Java's Collection containers, for example an ArrayList, can only store object references.
    • We can't store primitives like int or float on them.
    • Unless we use their corresponding primitive wrapper: Integer or Float, etc.
    • Unless it's an immutable class, there's no much logic filling up a Collection w/ the same reference.
    • Before populating a Collection, we gotta add() objects to it. Since it starts off empty!

    Nevertheless, I've come up w/ this weak solution for ya:

    // forum.processing.org/two/discussion/6717/
    // passing-variable-name-to-method
    
    import java.util.Collection;
    import java.util.List;
    
    void setup() {
      List<Integer> integers = new ArrayList();
      populateCollection(integers, (int) random(1, 3), 10);
      println(integers);
      println();
    
      List<Float> floats = new ArrayList();
      populateCollection(floats, random(-PI, TAU), 5);
      println(floats);
      println();
    
      exit();
    }
    
    static final <N extends Number> void populateCollection(
    Collection<N> container, N value, int quantity) {
      for ( int i = quantity; i-- != 0; container.add(value) );
    }
    

    P.S.: This comment link below got other examples about using multi-types in methods:
    http://forum.processing.org/two/discussion/comment/23775#Comment_23775

  • edited August 2014

    BtW, if a List is already populated and we just wanna "reset" it to some arbitrary value,
    we can apply Collections.fill() method on it:
    http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#fill-java.util.List-T-

      java.util.Collections.fill(floats, EPSILON);
      println(floats);
    
      java.util.Collections.fill(floats, null);
      println(floats);
    
  • edited August 2014 Answer ✓

    A more complete example, including an overloaded String version: O:-)

    /**
     * Multi-Type Value Fill (v1.0)
     * by GoToLoop (2014/Aug)
     *
     * forum.processing.org/two/discussion/6717/
     * passing-variable-name-to-method
     */
    
    import java.util.Collection;
    import java.util.Collections;
    import java.util.List;
    
    void setup() {
      List<Integer> integers = new ArrayList();
      populateCollection(integers, (int) random(1, 3), 10);
      println(integers);
      println();
    
      List<Float> floats = new ArrayList();
      populateCollection(floats, random(-PI, TAU), 5);
      println(floats);
    
      Collections.fill(floats, EPSILON);
      println(floats);
    
      Collections.fill(floats, null);
      println(floats);
    
      List<String> words = new ArrayList();
      populateCollection(words, "Populated", 3);
      println();
      println(words);
    
      Collections.fill(words, "Filled up!");
      println(words);
    
      exit();
    }
    
    static final <N extends Number> void populateCollection(
    Collection<N> container, N value, int quantity) {
      for ( int i = quantity; i-- != 0; container.add(value) );
    }
    
    static final <S extends CharSequence> void populateCollection(
    Collection<S> container, S word, int quantity) {
      for ( int i = quantity; i-- != 0; container.add(word) );
    }
    
  • class Something {
      ArrayList<CustomDatatype>[] things = new ArrayList<CustomDatatype>[3];
    
      Something() {
        for (int i = 0; i < things.length; i++) {
          things[i] = new ArrayList<CustomDatatype>();
        }
      }
    
      public void addToList(int idx, CustomDatatype data) {
        things[idx].add(data);
      }
    }
    
  • You could also use a Map of variable names to the corresponding ArrayList, but that seems a bit hackish imho.

  • edited August 2014

    Wow these are well informed replies, guys, thanks a lot. It's mostly rocket science to me though (I'm just a graphic designer with no particular coding education), so I'll find some awkward workaround using standard arrays or something else.

    I think I found the answer I think I was looking for in [this thread]http://forum.processing.org/two/discussion/6739/dynamically-generate-arraylists(this thread), by Quark: You cannot dynamically name variables in Java [...]. I believe it somehow has something to do with my original request.

    EDIT: argh, how do you post clean URL links here??

  • edited August 2014 Answer ✓

    It's true that you can't programatically name variables. But my suggestion of using a Map comes pretty close. Here is a short example that uses a Map of Strings (variable names) to Strings. You might use a Map of Strings to ArrayLists.

    Map<String, String> myMap = new HashMap<String, String>();
    
    void setup(){
       myMap.put("one", "first String");
       myMap.put("two", "second String");
       myMap.put("three", "third String");
    }
    
    void draw(){
       println(myMap.get("one");
    }
    

    More info in the API: http://www.processing.org/reference/HashMap.html

  • edited August 2014

    ... how do you post clean URL links here??

    If you just paste a full URL, that is, w/ http:// and such on it, the forum automatically recognizes it!
    Another way is highlight something and hit CTRL+L, then paste the URL.
    It forces the highlighted portion to become a link!

  • edited August 2014

    AFAIK, Processing 2+ doesn't auto import Map.
    Either declare myMap as a HashMap or add this statement up:
    import java.util.Map;

Sign In or Register to comment.