# Porting p5.processing Arraylist question

**URL:** https://discourse.processing.org/t/porting-p5-processing-arraylist-question/11869
**Category:** Coding Questions
**Created:** [June 5, 2019, 6:23pm UTC](https://discourse.processing.org/t/porting-p5-processing-arraylist-question/11869 "2019-06-05T18:23:41Z")
**Posts on this page:** 4
**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: [June 5, 2019, 6:23pm UTC](https://discourse.processing.org/t/porting-p5-processing-arraylist-question/11869/1 "2019-06-05T18:23:41Z")

</div>

I’m trying to port over some code, from p5 ( this is the second environment I’ve learned to code in, the first being Khan academy). So I don’t understand all the concepts that Java utilises. However what seems like a simple task in p5, seems impossible to do in Processing.

What I’m looking for is a way of adding two items to the arraylist. The program is essentially searching through an arraylist called “hist”, and I want to know is at what index the condition I’m looking for is true, and return the object in the arraylist but also the “next” item I’m looking for. I’ve managed to make it work, however I don’t understand how to return the array.

However I get the following error.

cannot convert from sketch\_190506b.Cell[] to sketch\_190506b.Cell

I understand Processing has strict type requirements for return, I’m just not sure what is required though.

```auto
Cell randomNth(ArrayList<Cell>a){
 ArrayList<Cell [] > t = new ArrayList<Cell [] >();;
    for(int i=0;i<a.size();i++){
      Cell next = a.get(i).nthPass();
    if(next != null){
      Cell [] temp = {next,a.get(i)};
      t.add(temp);
    }
  }
    if(t.size()-1>0){
        int r = round(random(0,t.size()-1));
        return t.get(r);
    }else{return null;}

};

// --------------------- Original p5 code;

function randomNth(a){
  var t = [];
    for(var i=0;i<a.length;i++){
      var next = a[i].nthPass();
    if(next){
      t.push([a[i],next]);
    }
  }
    if(t.length>0){
        var r = round(random(0,t.length-1));
        t[r][0].nthPass();
        t[r][0].score = 1;
        t[r][1].score = 1;
        // t[r][0].score2 ++;
        // t[r][1].score2 ++;
        if(!t[r][0].neighbour_cluster[0]){
        t[r][0].neighbour_cluster[0] = t[r][1].id;

        if(!t[r][1].neighbour_cluster[0]){
          t[r][1].neighbour_cluster[0] = t[r][0].id;
        }
        else{
          t[r][1].neighbour_cluster[1] = t[r][0].id;
          t[r][1].score2 = 2;
        }
        }
        else{
          t[r][0].neighbour_cluster[1] = t[r][1].id;
          t[r][0].score2 = 2;
          if(!t[r][1].neighbour_cluster[0]){
            t[r][1].neighbour_cluster[0] = t[r][0].id;
          }
          else{
            t[r][1].neighbour_cluster[1] = t[r][0].id;
            t[r][1].score2 = 2;
          }
        }
        neighbouringCluster(t[r][0]);
        neighbouringCluster(t[r][1]);
        return t[r];
    }else{return false;}

};

```

---

<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: [June 5, 2019, 6:29pm UTC](https://discourse.processing.org/t/porting-p5-processing-arraylist-question/11869/3 "2019-06-05T18:29:41Z")

</div>

Hey there!

But then how would I return the array?

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [June 5, 2019, 6:34pm UTC](https://discourse.processing.org/t/porting-p5-processing-arraylist-question/11869/5 "2019-06-05T18:34:41Z")

</div>

Sorry my bad ! Change the method to Cell[] type not Cell because you are returning an Array.

---

<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: [June 5, 2019, 6:35pm UTC](https://discourse.processing.org/t/porting-p5-processing-arraylist-question/11869/6 "2019-06-05T18:35:36Z")

</div>

Aha gotcha, amazing!!

Processing is a whole other kettle of fish…
