Calling method from arraylist.

I'm trying to create an object in an ArrayList and then call a method from that object. I get the error "the function 'all sounds' does not exist", even though it does exist within the class Snap_waves.

What am I doing wrong?

(shortened to the relevant code)

    ArrayList wb = new ArrayList();
    void setup() {
    size(640,720)
    }

    //this conditional happens within a button so it's not constantly loading.
     if (stageID==10) {
            wb.add( new Snap_waves());
    }

    void compartments(){
        if (stageID_highlight==10) {
        wb.get(0).allSounds();
      }
Tagged:

Answers

  • edited December 2017

    ArrayLists do not remember the type of thing stored in them, so when you get an object back from .get(), you either need to cast it to the kind of object you want, or tell the ArrayList what sort of objects it is storing. The easiest fix is thus to change your first line:

    ArrayList<Snap_waves> wb = new ArrayList();
    

    This will tell the ArrayList that it's holding Snap_waves objects. You can read more about this in the reference.

  • Thanks for your help. What if I need it to load multiple kinds of objects? I have about 10 others and they'd be loaded one or two at a time.

  • I would suggest that you have one ArrayList for each (collection of Object)s that you have. That is, group your objects by type, and have one ArrayList for each group.

Sign In or Register to comment.