Porting p5.processing Arraylist question

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.

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

};
1 Like

Hey there!

But then how would I return the array?

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

1 Like

Aha gotcha, amazing!!

Processing is a whole other kettle of fish…

1 Like