Pet Peeve With Array InitializationnTip
in
Share your Work
•
2 years ago
This is something that crops up once in a great while for me and has always annoyed me.
The problem: you can only do a curly brace array initialization at the point you declare your array - note that this is a Java thing.
So the following code will crash and burn:
There are times when I need a global array but can't initialize it at that moment and the curly braces make for a nice shortcut. My way around the problem is to do the following:
Best Regards, Jim
The problem: you can only do a curly brace array initialization at the point you declare your array - note that this is a Java thing.
So the following code will crash and burn:
- float[] xx1;
- void setup() {
- size(100, 100);
- xx1 = { width/2, height/2,1,2,3};
- }
There are times when I need a global array but can't initialize it at that moment and the curly braces make for a nice shortcut. My way around the problem is to do the following:
- float[] xx1;
- void setup() {
- size(100, 100);
- float [] xx2 = { width/2, height/2, 1,2,3};
- xx1=xx2;
- }
Best Regards, Jim