Pass arrayList of objects to function

cv_cv_
edited January 2016 in How To...

Hi! I am just getting started with processing so this is a potentially silly question (first post, HURRAY!).

I have a function that expects an ArrayList full of objects to iterate through, however I would like this function to be able to handle any object within my sketch and I am not quite sure on how to achieve it. When I iterate through the list I need to make a temporary object to hold the values of the iterated object so that is stopping me from being able to feed any object into the list, is that right? I could post some code if it helps clarifying...

I look forward to hear your solutions.

Answers

  • I could post some code if it helps clarifying...

    W/o code, we can only guess what's going on. Here's how far I can help ya:

    // forum.Processing.org/two/discussion/14340/
    // pass-arraylist-of-objects-to-function
    
    // GoToLoop 2016-Jan-08
    
    import java.util.List;
    
    static final <T> List<T> processList(List<T> l) {
      for (final T item : l) { 
        print(item, TAB);
      }
    
      return l;
    }
    
  • cv_cv_
    edited January 2016

    Hey! Thank you very much. I am pretty new at this and its very likely I am doing something stupid. I have never programmed anything like this before, just trying to figure it out as I go. Here is the code. This function works with an object called Hormone, I would like it to make it work with all objects in all arrayLists in my program. The essential bit is really inside the do while loop (Hormone myHormone = food.get(counter);)

    Hope it is sort of clear, thank you for your help! Meanwhile would you mind explaining a little bit what is happening with your code?

            Hormone myHormone = food.get(counter);   // get a copy of the object
            counter++;                              
            dist = PVector.dist(randomPos, myHormone.position);
    
  • Answer ✓
        Hormone myHormone = food.get(counter);   // get a copy of the object
        counter++;                              
        dist = PVector.dist(randomPos, myHormone.position);
    

    all you use of the Hormone is 'position'. is this going to be true of all the other objects you plan to use? because if it is then there's a mechanism in java for this called interfaces

    in short, you define a set of methods (and / or members?) that are common in an interface and then define your classes as implementing that interface... you can then have a list of any mix of these objects and the compiler knows that it can safely call any of the defined methods on any of the objects in that list.

    https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html

  • cv_cv_
    edited January 2016

    Hey! I couldn't really find that submit from before, my bad! @koogs: yes, all my objects will have a 'position', it seems that the interface is a bit more achievable for my level of programming for now. I will be looking into the answers and hopefully be able to sort it out. Fantastic help! thank you very much

  • So... I have been playing with the code and this is what I am at.

    I created an interface that holds a method getPosition()

    interface AbsAgent {
     void display();
     PVector getPosition(); 
    } // end of interface
    

    every class in the sketch is then class whateverClass implements AbsAgent, and has an implementation of the method getPosition()

    I am still struggling to understand how to integrate it fully in the sketch though. My function dartThrow() finds a suitable position for placing ann object given certain conditions, and I would like it to work to all classes in my sketch.

    so back to my iteration

    Hormone myHormone = food.get(counter);   // get a copy of the object
    counter++;                              
    dist = PVector.dist(randomPos, myHormone.position);
    

    I thought I could then pass to my function the ArrayList I am working with, like so:

    ArrayList <Agent> agents = new ArrayList<Agent>();
    ArrayList <Hormone> food = new ArrayList<Hormone>();
    
    dartThrow(agents);
    dartThrow(food);
    
    PVector dartThrow(ArrayList myArray) {
    ...
    AbsAgent myAgent = myArray.get(counter);
    counter++;                             
    dist = PVector.dist(randomPos, myAgent.getPosition());
    ...
     }
    

    but I keep getting the error cannot convert from object to mySketch.AbsAgent. It is probably me misunderstanding how to use this constructs, but I can't believe I really need to duplicate my code to handle two Arraylists of different types

  • did you look at my solution?

  • hey @Chrisir, I did look at your solution and the code I came up with comes a lot from there. However I dont think I should be creating a single ArrayList to deal with all my objects, so I would still like to make it work in different arraylists. is that silly? If I join all the object types in one ArrayList it will end up in a never ending check of objective everywhere in the sketch, maybe I am not thinking about it correctly?

  • actually I don't understand my own code fully.

    but isn't the idea here that the interface encapsules the two different classes so we could say display which in turn means different things for both classes

    so you need no checks

  • yes, that is exactly it, and it is what I am trying to do. my Interface has a getPosition() method that is defined in all my classes, so I thought I could say

    interfaceMethod anyObject = myArray.get(i);
    anyObject.getPosition();
    

    but I am not being very lucky, or maybe just really missing the point. any chance you can post some pseudo code mate?

  • here you see the handling of readX

    // mcve for interface with different classes
    
    // the nodes 
    ArrayList<Node> nodes = new ArrayList();
    
    // --------------------------------------------
    // Core functions 
    
    void setup() {
      size(900, 800);
    
      // add new EllipseClass/RectClass with random color 
      color randomColor = color(random(0, 255), random(0, 255), random(0, 255));
    
      nodes.add ( new EllipseClass(44, 44, 
        randomColor));
      nodes.add ( new EllipseClass(111, 111, 
        randomColor));
      nodes.add ( new RectClass(211, 211, 
        randomColor));
    } // func 
    
    void draw() {
      // clear screen 
      background(255);
    
      // for-loop
      for (int i = 0; i < nodes.size (); i++) { 
        Node someClass  = nodes.get(i);
        someClass.display();
        println(someClass.readX());
      } // for 
    
      if (frameCount>60&&frameCount<64) {     
        Node someClass  = nodes.get(1);
        someClass.incrementXY(22, 0); // change position 
        someClass.incrementXY(33, 0);
      } // else
    } // func 
    
    // ==============================================
    // interface and classes
    
    class RectClass implements Node {  
    
      // pos
      float x=0;
      float y=0;
    
      // color 
      color RectClassColor=0; 
    
    
      // constr 
      RectClass(float tempX, float tempY, 
        color tempRectClassColor) {
        x = tempX;
        y = tempY;
    
        RectClassColor = tempRectClassColor;
        //
      } // constr
      //
      void display() {
        fill(RectClassColor);
        rect (x, y, 16, 16);
      }
    
      void setXY(float x_, float y_) {
        x=x_;
        y=y_;
      }
    
      void incrementXY(float x_, float y_) {
        x+=x_;
        y+=y_;
      }
    
      float readX() {
        return x;
      }
      //
    } // class
    
    // -------------------------
    
    class EllipseClass implements Node {  
    
      // pos
      float x=0;
      float y=0;
    
      // color 
      color RectClassColor=0; 
    
    
      // constr 
      EllipseClass(float tempX, float tempY, 
        color tempRectClassColor) {
        x = tempX;
        y = tempY;
    
        RectClassColor = tempRectClassColor;
        //
      } // constr
      //
      void display() {
        fill(RectClassColor);
        ellipse (x, y, 15, 15);
      }
    
      void setXY(float x_, float y_) {
        x=x_;
        y=y_;
      }
    
      void incrementXY(float x_, float y_) {
        x+=x_;
        y+=y_;
      }
    
      float readX() {
        return x;
      }
      //
    } // class
    
    
    interface Node {
    
      // Let's abstract
    
      // Let's abstract display()
      void display();
      void incrementXY(float x_, float y_);
      float readX();
    }
    //
    
Sign In or Register to comment.