Adding data to a string array how to?

Hello, I am trying to add data to a string array every time one of those data values changes. I don’t know if this is possible.

time = timeStamp.format(now);
  for (int i = 0; i < 2; i++) {
    String[] gateGageTime;
    gateGageTime[i] = append(gateGageTime[i],time);
    //gateGageTime[i] = time;
    //print(gateGageTime[i]);
  }
  print(gateGageTime);

Everything I try keeps giving me errors. Is there a way to save a string into an array, and change it? All I can find are ways to append or shorten string arrays.

Use an arraylist! That is much more dynamic than String arrays :smiley:

Instead of:

String[] gateGageTime;

Make an arraylist like this:

ArrayList<String> gateGageTime = new ArrayList();

And then, when you want to add a new string to that arraylist? All you have to do is:

gateGageTime.add(time);

And you can print out your arraylist as you would normally print out anything else to see what is inside, just like this:

println(gateGageTime);

If you want to access the string later? All you have to do is:

gateGageTime.get(the_index_you_want_to_get);

You can also manipulate the String object inside directly like this for example:

String temp = gateGageTime.get(0).substring(0,1); // taking the substring of the String at index zero in your gateGageTime arraylist

Then when you are done doing that, you can add it right back to the arraylist wherever you want!

gateGageTime.set(0, temp); //zero is the index you are setting

Hopefully that can get you started, and don’t mind posting code to keep us posted on your progress! If you have any questions, I would be glad to answer them.

EnhancedLoop

Adding to @EnhancedLoop7 comment, a list is more suitable data container as it grows dynamically. An array of Strings can be used for the job but it will not be the right tool to do this. I would use a fixed length array if I knew my list will not grow dynamically. If it needs to grow, then a list is better because:

  • The code to grow the list is already implemented for you, so no need to reinvent the wheel
  • It will be bug free, and less time addressing bugs that comes when implementing your own structures.

This is just a recommendation. Feel free to explore the reference to discover what other tools Processing offers you. I seem to find one every time I browse their list (I keep forgetting they exist in the first place)

Kf