Situation is as this:
class Displayer retrieves data from twitter API.
The data may or may not contain an array of needed (when available) indexes.
so I'm doing
class Displayer{
int[] indexes;
Displayer() // constructor
{
possibleArray = getPossibleArray() // its a library function that does not returns null. returns an array of 0 length.
if(possibleArray > 0)
{
indexes = new int[possibleArray.length];
for( traverse the array... )
{
indexes[i] = possibleArray[i];
}
}
}}
indexes will be used later...
So if there is no array, indexes will be declared but not initialized. Is this ok? Does it waste memory? If i do not decalre it as a member variable and wait to see if this instance will need it (declare only if needed inside constructor) i cant use it later... So how to handle that?
Also that's a little dangerous cause i'll have to check always when using those data if it exists. Actually, now in the sketch i have moved the
new int[size] out of (
length >0 test). So i create an array always even if zero length. So existent code don't break later. But seems waste of resources...
thx
EDIT: If i try
int[] i ;
println(i);
or
int[] i ;
if( i != null);
println(i);
won't compile cause "var may not have been initialized". That probably answers my first question... But still, what's the best approach. Do i need to create all this empty arrays?
1