We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
empty array (Read 791 times)
empty array
Jan 3rd, 2010, 6:38am
 
Hello,

I am totally new into Processing and I have a basic question:

I am modifying an excising sketch, and I would like to empty an array:

Code:

byte[] photo = {};


How is this done?

Thanks in advance!!

Cheers.
Re: empty array
Reply #1 - Jan 3rd, 2010, 8:49am
 
When you want an array there are 2 stages to go through i.e.
Code:

// Declare an array called b but do not create it. at this
// moment b will have the value null
byte[] b;

// Now use b to create an array of 100 elements
// each element being 1 byte
b = new byte[100];

b will no longer be null because it references the array of 100 bytes.

Now we come to the crunch because it depends on what you meant by
Quote:
I would like to empty an array:


(1) We can loop through all 100 elements setting them to some value e.g. 0. The array would still exist.
(2) We can set the size of the array to 0 i.e.
Code:
b = new byte[0] 


the array reference will still exist but the array has no (zero) elements so is truly empty.
(3) We can simply set b to null i.e.
Code:
b = null; 


the array no longer exists.

In the last 2 cases attempting to use b is pointless except to create a new array.
Re: empty array
Reply #2 - Jan 3rd, 2010, 12:13pm
 
Ok great Smiley

I think ill use:
Code:

b = null;


to delete the array and then redefine it in the next cycle:

Code:

byte[] photo = {};


Thanks.
Page Index Toggle Pages: 1