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.
IndexProgramming Questions & HelpSyntax Questions › strange behavior of subset with object arrays
Page Index Toggle Pages: 1
strange behavior of subset with object arrays (Read 383 times)
strange behavior of subset with object arrays
Nov 29th, 2007, 9:00pm
 
Hello world,

I have experienced a strange behavior of the subset() function, when used in conjunction with object arrays..

It seems the subset(array, offset) syntax doesn't work when the array consists of objects. A workaround is to always use the subset(array, offset, length) syntax.

See code example below.

Can anyone explain this, or is this perhaps a bug..

regards prinds

Code:

void setup()
{
println("----- test 1 ------");
int[] numbers = new int[3];
numbers[0] = 90;
numbers[1] = 150;
numbers[2] = 30;

// subset works fine with normal arrays
int[] somenumbers = subset( numbers, 1 );

for( int i=0; i<somenumbers.length; i++ )
{
println(i + ", " + somenumbers[i]);
}

println("----- test 2 ------");

Test[] tests = new Test[3];
tests[0] = new Test(90);
tests[1] = new Test(150);
tests[2] = new Test(30);

// without the length parameter subset unexpectedly return the first two elements of the array
Test[] sometests = (Test[]) subset( tests, 1 );

for( int i=0; i<sometests.length; i++ )
{
println(i + ", " + sometests[i].val);
}


println("----- test 3 ------");

// adding the length parameter makes the subset function work
Test[] somenewtests = (Test[]) subset( tests, 1, 2 );

for( int i=0; i<somenewtests.length; i++ )
{
println(i + ", " + somenewtests[i].val);
}

}

class Test
{
int val;
Test( int val )
{
this.val = val;
}
}

Re: strange behavior of subset with object arrays
Reply #1 - Nov 30th, 2007, 12:27am
 
just a dumb mistake in the code for subset. now fixed for 0136, thanks for looking into it.
Page Index Toggle Pages: 1