Coming from a p5.js background, and I understand the use of slicing to make a copy of an array. This is done as setting a variable to the array you wish to copy makes a link which will modify the original arrays values should you call functions on the array indices or if you then should to remove some of these it will also remove them from the original.
So my question how can I achieve the same effect in processing. The issue I have is I can use clone, but this only creates a shadow copy, and so setting the variable type doesn’t work, and therefore I get an error. of course simply setting one new arrayList variable to the arrayList I wish to copy has the same behavioral issues as its p5 counterpart.
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.iid);
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;
};
so this code technically works fine, but in the end I’m left with a completely empty arrayList, and as this is the one which contains all my Objects I cannot afford to delete it.