Passing arrays as arguments

How are arrays passed as arguments into objects? Do i need to create a new array in the object to accept the array data?

1 Like

i hope that i understand, not a array of class?

// https://discourse.processing.org/t/passing-arrays-as-arguments/6692

int[] myR = { 10,20,30,40,50 };

MyCircle ring = new MyCircle(0,0,myR);;

void setup() {
  size(200,200);
}

void draw() {
  background(200,200,0);
  translate(width/2,height/2);
  ring.drawit();  
}

class MyCircle {
  int posX,posY;
  int[] radius;
  MyCircle(int posX, int posY, int[] radius ) {
  this.posX = posX;
  this.posY = posY;
  this.radius = radius;
  }
  void drawit() {
   stroke(0,200,0);
   noFill();
   for (int i = 0; i<radius.length;i++) ellipse(posX,posY,radius[i],radius[i]);
  }
}

1 Like

Thank you kll. You understood correctly and, answered my question on how to pass arrays. Your example passes the array as a reference. The tutorials on arrays or objects doesn’t cover passing arrays as arguments. However your example does. It is worth noting that one must be careful about editing passed arrays, because that edit actually edits the original array.
Thanks

This is a misunderstanding – there is a subtle but important distinction. Arrays are objects. Java passes a value – and that value is a reference to the object. For discussion, see for example:

The important thing to understand here is that arrays like float[], Processing objects like PVector, Java objects like HashMap and your own custom objects class MyObject all follow the same rules. You pass in a value that is a reference, which you can use to access and update the object, but you cannot manipulate it as a reference – it is just a value.

A classic example of the consequences of this is badSwap(). If Java was pass by reference, badSwap() would swap the references – but it isn’t, so it can’t, and it doesn’t.

Java does manipulate objects by reference, and all object variables are references. However, Java doesn’t pass method arguments by reference; it passes them by value. https://www.javaworld.com/article/2077424/learn-java/learn-java-does-java-pass-by-reference-or-pass-by-value.html

Take the badSwap() method for example:

public void badSwap(int var1, int var2)
{
  int temp = var1;
  var1 = var2;
  var2 = temp;
}

For arrays / objects, try this:

void setup(){
  float[] vars1 = new float[]{1.0, 2.0};
  float[] vars2 = new float[]{3.0, 4.0};
  badSwap(vars1, vars2);
  println("global results:");
  println(vars1);
  println(vars2);
}

public void badSwap(float[] vars1, float[] vars2) {
  println("args:\n", vars1, vars2); // yep, those are reference values
  // we can use them to update the referenced objects
  vars1[0] = 100;
  // ...but we can't manipulate global references, because we only have their values
  // we swap the values locally
  float[] temp = vars1;
  vars1 = vars2;
  vars2 = temp;
  // ...but they aren't references, so that had no effect on the global objects
  println("new local values:\n", vars1, vars2);
  // and this won't do anything globally either -- these are just local values:
  vars1 = null;
  vars2 = null;
  println("new local values:\n", vars1, vars2);
}
3 Likes

Hey, thanks for the clues on this.
I ended up creating a different solution to solve the problem myself - as I couldn’t get the swap method to work.

I converted dynamic array values to static in the constructor when instantiating each object in my class.
Hope this helps anyone trying to sample live data. In my case I am freezing/sampling audio data into a waveform.

class Line {
      float[] staticWave = new float[2048];
      Line(float[] wave){
        for(int i=0; i < wave.length; i++){
          staticWave[i] = wave[i];
        }
1 Like