We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › ArrayList question
Page Index Toggle Pages: 1
ArrayList question (Read 627 times)
ArrayList question
Apr 23rd, 2009, 8:31am
 
I'm newbie.  When I try to run this sketch, it gave me "Cannot convert from Object to int" on the line p = pitches.get(i);
Also, I wonder how to declare and initialize an ArrayList in a class.  I wonder if I actually did it the right way.  Many thanks in advance for your help.
Quote:
class ChanLite
{
    int x;
    int y;
    ArrayList pitches;
    
    ChanLite(int ix, int iy) {
      x = ix;
      y = iy;
      pitches = new ArrayList(5);
    }
    
  void display(int pit, int vel) {
        int tr, tg, tb, p = 0;
        pitches.add(pit);
        for (int i = pitches.size()-1; i >=0; i--) {
          p = pitches.get(i);
          tr = tr + red(c[p%12]);
          tg = tg + green(c[(int)pitches.get(i)%12]);
          tb = tb + blue(c[(int)pitches.get(i)%12]);
        }
        fill(tr*vel/128/pitches.size(), tg*vel/128/pitches.size(), tb*vel/128/pitches.size());
        rect(x, y, 20, 20);
  }
  
  void remove(int pit) {
    // pop    
        int tr, tg, tb = 0;
        pitches.add(pit);
        for (int i = pitches.size()-1; i >=0; i--) {
          if (pitches.get(i) == pit) {pitches.remove(i);} else {
          tr = tr + red(c[pit%12]);
          tg = tg + green(c[pit%12]);
          tb = tb + blue(c[pit%12]);}
        }
        fill(tr*vel/128/pitches.size(), tg*vel/128/pitches.size(), tb*vel/128/pitches.size());
        rect(x, y, 20, 20);
  }
}

Re: ArrayList question
Reply #1 - Apr 23rd, 2009, 8:39am
 
Try casting to Integer rather than int.

An Integer is an object (so it can be cast to another object) while an int is a primitive (just a number).


As for the initialization, it is fine (probably best) as it is; in the constructor.

If you initialize it as
ArrayList pitches = new ArrayList(5);

then all of your ChanLite's would share the same 'pitches'.
Re: ArrayList question
Reply #2 - Apr 23rd, 2009, 9:00am
 
Thanks, NoahBuddy!  (Integer) is it.
However, now I get "cannot cast from float to integer" error.  (The c is declared as color[] c; and initialized elsewhere.  This is the first time I tried putting this class in a separate tab/file -- do I need to declare it here too?)

       for (int i = pitches.size()-1; i >=0; i--) {
         p = (Integer) pitches.get(i);
         tr = tr + red(c[p%12]);
         tg = tg + green(c[p%12]);
         tb = tb + blue(c[p%12]);
       }
Re: ArrayList question
Reply #3 - Apr 23rd, 2009, 9:21am
 
Ah, that is because red(), blue(), and green() return a float.

Your t* variables are int's. :]
Re: ArrayList question
Reply #4 - Apr 23rd, 2009, 9:48am
 
Thanks again, NoahBuddy!  I discovered it right after posting too.
Page Index Toggle Pages: 1