Expand an array?
              in 
             Programming Questions 
              •  
              2 years ago    
            
 
           
             Hi,
            
            
Once an array has been initialised at a certain size, how can you empty and change the size of the array? I'm working on this problem in a larger program, so I've written this simple bit of code to try and figure out how to do it. What I want it to do is set up an array of a particular size, and then whenever the mouse button is pressed, change the size of that array to the mouseX position. It doesn't seem to work, and I can't figure out what I'm doing wrong. Sorry, I've only been coding about two weeks...
            
            
Any help MUCH appreciated!
            
Steve
            
 
           
 
            
           Once an array has been initialised at a certain size, how can you empty and change the size of the array? I'm working on this problem in a larger program, so I've written this simple bit of code to try and figure out how to do it. What I want it to do is set up an array of a particular size, and then whenever the mouse button is pressed, change the size of that array to the mouseX position. It doesn't seem to work, and I can't figure out what I'm doing wrong. Sorry, I've only been coding about two weeks...
- int Asize = 100;
 int[] testarray = new int[Asize];
 void setup() {
 size(400, 400);
 }
 void draw() {
 }
 void mousePressed() {
 println("The previous array size was " + testarray.length);
 Asize = mouseX;
 expand(testarray, Asize);
 println("The new array size is " + testarray.length);
 }
Any help MUCH appreciated!
Steve
 
              
              1  
            
 
            
 
 
           
 
            