[SOLVED] Slicing arrayList

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.

1 Like

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

Another option if you know the beginning and end index and just want a slice of an ArrayList: use .subList()

https://docs.oracle.com/javase/1.5.0/docs/api/java/util/List.html#subList(int,%20int)

import java.util.List;
ArrayList<String> testList;

testList = new ArrayList<String>();
testList.add("foo");
testList.add("bar");
testList.add("baz");
testList.add("ack");

List mySlice = testList.subList(1,3);
println(testList);
println(mySlice);

output:

[foo, bar, baz, ack]
[bar, baz]

If you are using a primitive array instead, use subset()

https://processing.org/reference/subset_.html

1 Like