declaration of array of objects, which method?
in
Programming Questions
•
2 years ago
Hello,
I discover the world of Processing, and I'm thrilled by it.
I have some questions regarding arrays of objects, the way to create them, and eventually discuss how to make arrays of variable lenght.
my way of declaring an array of object until now was:
suppose of course that the class Branch is defined
- int number_of_instances = 100; // size of the array to be created
- Branch[] _branchArr = new Branch[number_of_instances]; // declaration of the array
- for (int i=0; i<number_of_instances; i++) {
- _branchArr[i] = new Branch(); // constructor EDIT: I corrected the typo // found by phi.lho as described below
- }
Looking thru some source code at abandonnedart.com, I saw this form of declaration for arrays of objects:
- Branch[] _branchArr = {};
- // below the code is meant to create one new object in the array
- // it could be put in a for loop as above
- Branch br = new Branch();
- _branchArr = (Branch[])append(_branchArr, br);
in the first line I suppose {} stands for the NULL ensemble, that is an array is created that contains no element at all, am I right?
then this method seems to provide an easy way to make arrays of variable lenght, if needed (unlike my method which fix a number_of_instances ). (using also shorten() or expand() ).
Then I saw ArrayList existed, and it's purposes was to create array of changing size.
so my questions are:
- are there other methods to create arrays of objects, that would be interresting?
- is one method "better" than another to create arrays of objects? especially if one wants a variable lenght of array?
this last is probably too advanced:
- is there a risk to run out of memory, or else, creating to many objects (I guess so), and is there a way of detecting such a danger, and then provide to the user a dedicated limit, corresponding to it's own personnal situation?
Best regards,
Laurent.
1