Why won't append() increase the size of my array?

This code is designed to store the x & y values of mouse clicks in 2 arrays. I have set it up with an initial 3 elements and want the arrays to expand should there be more mouse clicks. On the 3rd mouse click I get an out of bounds exception:3 message. The append() instruction does not appear to have expanded the arrays. What have I done wrong?

boolean loaded=false; int[] verticex = new int[3]; int[] verticey = new int[3]; int nev=3; //number of elements in vertices

void draw() { if (loaded==false){ loadPixels(); bg.loadPixels(); updatePixels(); background(bg); loaded=true; } }//draw

//append array with each mouse click void mouseClicked() { if (nev<4){ for (int cv=0;cv<nev;cv++){ verticex[cv]=mouseX; verticey[cv]=mouseY; } }else{ append(verticex, verticex[0]); append(verticey, verticey[0]); verticex[0]=mouseX; verticey[0]=mouseY; verticex[nev-2]=mouseX; verticey[nev-2]=mouseY; } nev=nev+1; printArray(verticex); printArray(verticey);

Tagged:

Answers

  • edited April 2016 Answer ✓

    https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    General tip: if append() is needed, it means regular arrays aren't the right container!

    For whole values, a dynamic container such as IntList is the best option:
    https://Processing.org/reference/IntList.html

  • Thanks. Lists certainly look like a better option here than arrays so I have amended the code accordingly. It wants me to put curly brackets around the list declarations but if I do this or declare them in setup it comes back with the message 'variable doesn't exist'.

    PImage bg;
    void setup() {
      //size(518, 658); bg = loadImage("Owen3.png");
      size(357, 532); bg = loadImage("PortraitOwen2.png");
      colorMode(HSB,360,100,100);
    }
    
      boolean loaded=false;
      int nev=3; //number of elements in vertices
      verticex= new IntList();
      verticey= new IntList();
    
    void draw() {
      if (loaded==false){
      loadPixels();
      bg.loadPixels();
      updatePixels();
      background(bg);
      loaded=true;
      }  
    }//draw
    
    //append array with each mouse click
    void mouseClicked() {
      if (nev<4){
        for (int cv=0;cv<nev;cv++){
          verticex.append(mouseX);
          verticey.append(mouseY);
        }
      }else{
        verticex.append(verticex.get(1));
        verticey.append(verticey.get(1));
        verticex.set(0,mouseX);
        verticey.set(0,mouseY);
        verticex.set(nev-2,mouseX);
        verticey.set(nev-2,mouseY);    
      }
      nev=nev+1;
      println(verticex);
      println(verticey);
    }//mouseClicked
    
  • Answer ✓
    • In order to declare a variable, we need to specify its type before its name.
    • Exactly like you did for variable bg: PImage bg;
    • verticesX = new IntList(); isn't a declaration but an assignment!
    • Of course we can have both declaration + assignment: final IntList verticesX = new IntList();
  • Note that the final keyword will prevent allocating another list to verticesX anywhere else in your program. If this causes a problem then simply use.

    IntList verticesX = new IntList();

  • In this case I decided I didn't need the final keyword

Sign In or Register to comment.