Arraylist Function

Dear all,

I have the following code, which was extracted from a project in OpenProcessing. I was wondering if someone can explain the following code, as I don't understand how an Arraylist can be used like this - it looks like a function, which has a parameter and a return type, however this is assigned to an Arraylist. I don't understand what exactly this does, and more importantly I don't know how I can translate this into a P5.js code.

ArrayList<Bob> getNeighbors(float threshold) {
    ArrayList<Bob> proximityBobs = new ArrayList<Bob>();

    for (Bob otherBob : bobs) {
      if (this == otherBob) {
        continue;
      }

      float distance = dist(this.pos.x, this.pos.y, this.pos.z, 
                            otherBob.pos.x, otherBob.pos.y, otherBob.pos.z);
      if (distance < threshold) {
        proximityBobs.add(otherBob);
      }
    }
    return proximityBobs;
  }

ArrayList<Bob> proximityBobs = this.getNeighbors(120);

If someone can give me a tip as to how exactly this works, that would be excellent.

Thank you

Answers

  • You're probably familiar with functions like this:

    void doSomething(){
      //do something
    }
    

    The void keyword here means that the keyword has a void return type: in other words, it doesn't return anything.

    But you can also have functions like this:

    int returnSomething(){
      return 42;
    }
    

    This function has an int return type, which means it returns an int value.

    The getNeighbors() function in your code is returning an ArrayList<Bob> value.

    To translate code from one language to another, you need to understand what the code does first. What does this function do? Then do that in the target language.

    The only major difference is that P5.js doesn't have an ArrayList object. It doesn't need one, because JavaScript arrays are much more versatile than Java arrays.

  • edited February 2018

    Thank you @KevinWorkman for your answer. This is what I did (functions are inside a class)

    getNeighbors(threshold) {
        let proximityBlobs = [];
        let otherBlob = [objectArray.length];
    
        for (let i=0; i<objectArray.length; i++){
          otherBlob[i] = objectArray[i];
    
          if (this == otherBlob[i]) {
            continue;
          }
          let distance = dist(this.pos.x, this.pos.y, this.pos.z, otherBlob[i].pos.x, otherBlob[i].pos.y, otherBlob[i].pos.z);
          if (distance < threshold) {
            proximityBlobs.push(otherBlob[i]);
          }
        }
        return proximityBlobs;
      }
    
      draw() {
        let proximityBlobs = [];
        proximityBlobs = this.getNeighbors(120);
    

    I translated the above into the code here - I don't get any errors, but I am not sure if the for loops are the same. The original uses:

    for (Bob otherBob : bobs) {
    

    And I did the following to deep copy the original array:

    for (let i=0; i<objectArray.length; i++){
              otherBlob[i] = objectArray[i];
    

    I think its correct, not sure if this is the best way to do this.

  • Does the code do what you expected? Do you understand how it works? That's all that really matters.

    Like I said, you shouldn't translate code line-by-line. You should take what the code is doing and then reimplement that in your target language.

  • Is this for p5.js? Does p5 have an ArrayList function ... the reference just has standard arrays?

Sign In or Register to comment.