FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   enlarging an array
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: enlarging an array  (Read 551 times)
benelek

35160983516098 WWW Email
enlarging an array
« on: Jan 5th, 2003, 3:13am »

hello,
 
  is there a way to add more objects onto an array, once created? i've tried the ol' javascript method of
 
theArray[theArray.length]=newNumber;
 
without success.
 
the reason i ask is that i would like to create a global array of objects at the beginning of the program (ie, before loop()), and then tag extra things on the end of the array at any time.
 
is there an example i haven't seen, in which objects are created dynamically upon user-events (with or without an array)?
 
 
thanks,
 
-jacob
 
fry


WWW
Re: enlarging an array
« Reply #1 on: Jan 5th, 2003, 4:12am »

two answers.. first is that you can use the java class "Vector" which is a growable array.
 
personally, i find that too slow, so i use a bit of code like:
 
Code:
// grow an array for a list of strings
String list[];
int listCount;
 
void addToList(String what) {
  // if the array is full..
  if (listCount == list.length) {
    // double the size of the array
    String temp[] = new String[listCount*2];
    System.arraycopy(list, 0, temp, 0, listCount);
    list = temp;
  }
  // add the element to the list
  list[listCount++} = what;}

this will work for any sort of object or type.
 
it's also something i'd like to have as a feature of p5, since i'm tired of typing that bit of code for my own stuff.
« Last Edit: Jan 5th, 2003, 4:13am by fry »  
Pages: 1 

« Previous topic | Next topic »