Android Question Generic list

rameshb

Member
Hi,
Is it possible to create a generic list in Basic4Android?..
Existing List class is supporting only items of type object.
It will be useful if we can declare a list of the desired type like int,string etc. before using them.
This will save conversion time to/from object type to actual data type while using list.

Thanks in advance.
 

rameshb

Member
Yes we can store any value in list.But suppose when we are adding numbers to list,they will be converted to object before they are stored in list and while retrieving they need to be converted back to appropriate data type from object.
This is what we know as boxing/unboxing.
But in java we have generics to avoid this overhead of conversion.
What i mean is can we declare a generic list so that boxing/unboxing overhead is avoided.
Since B4A code is converted to java byte code while compiling,i hope there will be a way to do this.

Thanks in advance.
 
Upvote 0

kgwhite

Member
Licensed User
Longtime User
Yes we can store any value in list.But suppose when we are adding numbers to list,they will be converted to object before they are stored in list and while retrieving they need to be converted back to appropriate data type from object.
This is what we know as boxing/unboxing.
But in java we have generics to avoid this overhead of conversion.
What i mean is can we declare a generic list so that boxing/unboxing overhead is avoided.
Since B4A code is converted to java byte code while compiling,i hope there will be a way to do this.

Thanks in advance.

I agree Generics would be a step forward. They have been part of VB.Net for quite some time. Personally I wish B4A had stronger type handling in general. Implicit type casting makes me nervous.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is what we know as boxing/unboxing.
But in java we have generics to avoid this overhead of conversion.
This is not correct. The same thing happens in Java as well. This is how Java generics work (which is different than .Net generics).

You cannot create a List<int> in java, only List<Integer>.
 
Upvote 0
Top