Remove objects form arraylist and have them added to new arraylist

Hi. I have a question about some code. I have some code that other members were kind enough to help me with. Within the code a series of numbers are generated and then used to draw lines across the page. So for example the code might generate the numbers 1, 5, 10, 17 etc... 1 line will then be drawn in the top left corner of the page and then 5 lines drawn at a different point along the y axis etc...

What I would like to add to the code is the ability to remove random objects from the arraylist and then have them added to a new arraylist that would draw lines descending from the top right corner of the page if you press a key or something like that. So if you presses a key it would take a few lines from the left of the page and reposition them on the right.

Any help would be much appreciated. Thanks, Michael

ArrayList<Line> lines;

    int z = 0;
    int a = 0;
    int b = 0;

    int startx = 500;
    int starty = 500;
    int endy = 50;

    int[] numbers = new int[20];

    void setup() {

      size(1000, 1000);
      frameRate(4);
      lines = new ArrayList<Line>();

      int num =0;
      for (int x =0; x< 20; x++) {
        num +=int(random(10, 50));
        print((numbers [x]= (num % 60))  + ", ");
      }
      numbers = sort(numbers);

      println("\nsorted");
      println(numbers);
      fill(0);

    }

    void draw() {

      background(255);

      text("Current "+z,width/2,height*0.80);

      int nn=-1;
      for (int i=0; i<=z; i++)
        nn+=numbers[i] -1;

      generateLines();

      for (int i = 0; i <nn ; i++) {   
        Line myline = lines.get(i);
        myline.draw();

         }

      }


    void generateLines() {

      if (z < numbers.length -1 ) {
      z++; }


      for (int j=0;j<numbers[z] -1;j++){

        lines.add(new Line(startx, starty, random(50, 250), 50*z)); }

      } 

class Line {

  float sx;
  float sy;
  float ex;
  float ey;

  Line(float startX, float startY, float endX, float endY) {

    sx = startX;
    sy = startY;
    ex = endX;
    ey = endY;
  }

  void draw() {

    stroke(0);
    line(ex, ey, sx, sy);
  }
}

Answers

  • Thanks for the reply. However, I am still struggling. I have written code that removes objects from an arraylist before with .remove() however for some reason I just can't get it to work with this one. Also I would like to remove a random amount of objects and I'm not sure how to do that.

  • I have now updated the code. It almost does what I want it to do but is very buggy and unreliable, which is definitely down to my poor coding. It now (sometimes) removes items from the arraylist that is drawing lines on the left hand side of the page and then adds a random amount of objects to a new arraylist that draws lines on the right of the page when a key is pressed.

    However, it isn't quite doing what I want. I know the problem is within how the objects are being removed. At the moment I am just telling it to remove(0). However, I would ideally like a random amount of random objects to be removed instead.

    Any help in developing the code or making it work a but better would be great. Thanks

    ArrayList<Line> lines;
    ArrayList<Line> lines2;
    
        int z = 0;
         //int zz = -1;
        int startx = 500;
        int starty = 500;
        int endy = 50;
    
        int[] numbers = new int[21];
    
        void setup() {
    
          size(1000, 1000);
          frameRate(5);
          lines = new ArrayList<Line>();
          lines2 = new ArrayList<Line>();
    
          int num =0;
          for (int x =0; x < 21; x++) {
            num +=int(random(10, 50));
            print((numbers [x]= (num % 60))  + ", ");
          }
          numbers = sort(numbers);
    
          println("\nsorted");
          println(numbers);
          fill(0);
    
        }
    
        void draw() {
    
        background(255);
    
          text("Current "+z,width/2,height*0.80);
    
          int nn=-1;
          for (int i=0; i<= z; i++)
            nn+=numbers[i] -1;
    
          generateLines();
           generateLines2();
    
          for (int i = 0; i <nn ; i++) {   
            Line myline = lines.get(i);
            myline.draw(); }
    
    
     for (int z = 0; z < lines2.size() ; z++) {  
    
           Line myline2 = lines2.get(z);
            myline2.draw();
    
          }
          }
    
    
        void generateLines() {
    
          if (z < numbers.length -1 ) {
          z++; }
    
          for (int j=0;j<numbers[z] -1;j++){
    
            lines.add(new Line(startx, starty, random(25, 250), -25 + 50*z)); 
    
               if (keyPressed == true) 
                 lines.remove(0); 
          }
               }
    
            void generateLines2() {    
    
              if (keyPressed == true) {
                lines2.add(new Line(startx, starty, random(750, 975), -25 + 50*z));
          }
            }
    
    class Line {
    
      float sx;
      float sy;
      float ex;
      float ey;
    
      Line(float startX, float startY, float endX, float endY) {
    
        sx = startX;
        sy = startY;
        ex = endX;
        ey = endY;
      }
    
      void draw() {
    
        stroke(0);
        line(ex, ey, sx, sy);
      }
    }
    
  • edited February 2017

    Nevermind. I've managed to crack it...well sort of at least. The code now draws sets of lines along the left of the page and delete a random amount from the array list when you press a key. When the key is pressed a random number of lines are then drawn on the right of the page. I have another slight issue though will post it on a new discussion as it is a bit of a different topic. Thank you for your help.

    int [] coordinates = { 25,75,125,175,225,275,325,375,425,475,525,575,625,675,725,775,825,875,925,975 } ;
    
    ArrayList<Line> lines;
    ArrayList<Line> lines2;
    
        int z = 0;
        float zz = 0; 
        int startx = 500;
        int starty = 500;
        int endy = 50;
    
        int[] numbers = new int[21];
    
        void setup() {
    
          size(1000, 1000);
    
          lines = new ArrayList<Line>();
          lines2 = new ArrayList<Line>();
    
          int num =0;
          for (int x =0; x < 21; x++) {
            num +=int(random(1, 5));
            print((numbers [x]= (num % 60))  + ", ");
          }
          numbers = sort(numbers);
    
          println("\nsorted");
          println(numbers);
          fill(0);
    
        }
    
        void draw() {
    
        background(255);
    
          text("Current "+z,width/2,height*0.80);
    
          int nn=-1;
          for (int i=0; i <= z; i++)
            nn+=numbers[i] -1;
    
          generateLines();
           generateLines2();
    
          for (int i = 0; i < nn ; i++) {   
            Line myline = lines.get(i);
            myline.draw(); }    
    
     for (int z = 0; z < lines2.size() ; z++) {  
    
           Line myline2 = lines2.get(z);
            myline2.draw();
    
     }
          }
    
        void generateLines() {
    
          if (z < numbers.length -1 ) {
          z++; }
          for (int j=0;j<numbers[z] -1;j++){  
            lines.add(new Line(startx, starty, random(25, 250), -25 + 50*z)); 
    
            if (keyPressed == true) { 
    
          int randomIdx = (int) random(lines.size());
                 lines.remove(randomIdx);}
    
        }
        }
    
            void generateLines2() {    
    
           if (keyPressed == true) { 
    
                float xx = zz + random (10);
                for (int k=0; k < xx; k++){  
    
      int random = (int) random(20);
    
                lines2.add(new Line(startx, starty, random(750, 975), coordinates[random]));
        }
            }
        }
    
    
    class Line {
    
      float sx;
      float sy;
      float ex;
      float ey;
    
      Line(float startX, float startY, float endX, float endY) {
    
        sx = startX;
        sy = startY;
        ex = endX;
        ey = endY;
      }
    
      void draw() {
    
        stroke(0);
        line(ex, ey, sx, sy);
      }
    }
    
  • Answer ✓

    For use of remove you must loop backward through the array

    No joke.

    Also move lines 66 to 69 outside the for loop(which makes the first advice outdated)

    Remember to use ctrl-t in processing to get better format for indents

  • @MRoden1993

    Based on your initial program, if you removed a line object, then you have to make sure you update the right entry in your numbers array, otherwise you will have an out of bounds error. The changes you are making to your group of lines makes me think that you should be creating a single class that manages the group of lines. Are you familiar with OOP? You don't needed, but it is a suggestion.

    Kf

Sign In or Register to comment.