[SOLVED] Slicing arrayList

Simple solution, just use the the add and get function in a for loop.

  void init(){
    for(int i=0;i<lines.size();i++){
      this.unsorted_lines.add(lines.get(i));
    }  
  }
  
  void sort_lines(){
    while(this.unsorted_lines.size()>1){
        Line closest = this.closest(this.unsorted_lines);
        if(closest!=null){
          boolean a = this.sorted_lines.contains(closest);
          if(!a){
            this.sorted_lines.add(closest);
            int c = this.unsorted_lines.indexOf(closest);
            this.unsorted_lines.remove(c);
          }}}
          if(this.unsorted_lines.size()==0){
            this.ordered = true;
          }
  }
  
  Line closest(ArrayList<Line> a){
    
    Line b = this.unsorted_lines.get(0);
    for(int i=1;i<this.unsorted_lines.size();i++){
      Line c = unsorted_lines.get(i);
      float d1 = dist(b.mpx,b.mpy,this.mpx,this.mpy);
      float d2 = dist(c.mpx,c.mpy,this.mpx,this.mpy);
      if(d1>d2&&c!=this){  
        b = c;
      }}
      
      return b;
  };
2 Likes