2

I am trying to send an array over my serial port. The name of this array is 'send' and initially, it is empty. I want to append integers to this array but I am having issues doing so. I print the array, but it prints out nothing. Can someone help me out?

Thanks!

import processing.serial.*;

Serial myPort;
String val;

void setup(){
  size(200,200);
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);


}

void draw(){
    //myPort.write(0);
    //println(0);

}

int i = 0;
void mouseClicked(){
     //String myStr = "RGBY"; 
     String stre = "e|1";
     String strB = "B|1";
     String strG = "G|1";
     String strD = "D|1";
     String strA = "A|1";
     String strE = "E|1";

     println(stre);

     int[] send = {2};
     append(send, 1);
     printArray(send);
}
Mustafa
  • 337
  • 7
  • 14
  • Can you provide a [mcve]? – RaminS Mar 21 '17 at 20:59
  • Java arrays have a fixed length, you'll need to create a new array that is one longer (or use an `ArrayList`). – Elliott Frisch Mar 21 '17 at 21:00
  • @ElliottFrisch Please notice the [tag:processing] tag, and [Processing != Java](https://meta.stackoverflow.com/questions/321127/processing-java). The `append()` function is a Processing function that appends an item to an array, exactly how you're describing. – Kevin Workman Mar 22 '17 at 01:37
  • @ElliottFrisch Uhh, okay? I was just pointing out that this is a Processing question, which a lot of people miss because it's such a generic name. Not sure why that caused an attitude, but have a good day anyway. – Kevin Workman Mar 22 '17 at 01:44

2 Answers2

1

As per the reference, you should be saving the result of a function call:

send = append(send, 1);
Ivan
  • 3,781
  • 16
  • 20
0

In my case I needed to create an array of objects that would grow dynamically and the only way that worked was using ArrayList, it was perfect:

  ArrayList<MyObject> myObjects = new ArrayList<MyObject>();

Only then was I able to use the .add and .remove to resize the array dynamically.

Adding an object:

  objTemp = new MyObject(x, y);
  myObjects.add(objTemp);

Remove all objects:

  for (int i = myObjects.size() - 1; i >= 0; i--) {
    myObjects.remove(i);
  }
André Lima
  • 315
  • 4
  • 11