# \[SOLVED\] Slicing arrayList

**URL:** <https://discourse.processing.org/t/solved-slicing-arraylist/12974>\
**Category:** Coding Questions\
**Created:** [July 25, 2019, 5:29pm UTC](https://discourse.processing.org/t/solved-slicing-arraylist/12974 "2019-07-25T17:29:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [July 25, 2019, 5:29pm UTC](https://discourse.processing.org/t/solved-slicing-arraylist/12974/1 "2019-07-25T17:29:19Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [July 25, 2019, 6:04pm UTC](https://discourse.processing.org/t/solved-slicing-arraylist/12974/2 "2019-07-25T18:04:42Z")

</div>

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

```auto
  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;
  };

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [July 28, 2019, 6:42pm UTC](https://discourse.processing.org/t/solved-slicing-arraylist/12974/3 "2019-07-28T18:42:53Z")

</div>

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)](https://docs.oracle.com/javase/1.5.0/docs/api/java/util/List.html#subList(int,%20int))

```auto
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()`

> **[subset() / Reference](https://processing.org/reference/subset_.html)**
>
> Extracts an array of elements from an existing array. The list parameter defines the array from which the elements will be copied, and the start and count parameters specify which…
