First of all, I can only agree that Arrays.asList(T...)
is clearly the best solution for Wrapper types or arrays with non-primtive datatypes. This method calls a constructor of a simple private static AbstractList
implementation in the Arrays
class which basically saves the given array reference as field and simulates a list by overriding the needed methods.
If you can choose between a primtive type or a Wrapper type for your array, I would use the Wrapper type for such situations but of course, it's not always useful or required.
There would be only two possibilities you can do:
1) You can create a class with a static method for each primitive datatype array (boolean, byte, short, int, long, char, float, double
returning an Iterable<
WrapperType>
. These methods would use anonymous classes of Iterator
(besides Iterable
) which are allowed to contain the reference of the comprising method's argument (for example an int[]
) as field in order to implement the methods.
-> This approach is performant and saves you memory (except for the memory of the newly created methods, even though, using Arrays.asList()
would take memory in the same way)
2) Since arrays don't have methods (as to be read on the side you linked) they can't provide an Iterator
instance either. If you really are too lazy to write new classes, you must use an instance of an already existing class that implements Iterable
because there is no other way around than instantiating Iterable
or a subtype.
The ONLY way to create an existing Collection derivative implementing Iterable
is to use a loop (except you use anonymous classes as described above) or you instantiate an Iterable
implementing class whose constructor allows a primtive type array (because Object[]
doesn't allow arrays with primitive type elements) but as far as I know, the Java API doesn't feature a class like that.
The reason for the loop can be explained easily:
for each Collection you need Objects and primtive datatypes aren't objects. Objects are much bigger than primitive types so that they require additional data which must be generated for each element of the primitive type array. That means if two ways of three (using Arrays.asList(T...)
or using an existing Collection) require an aggregate of objects, you need to create for each primitive value of your int[]
array the wrapper object. The third way would use the array as is and use it in an anonymous class as I think it's preferable due to fast performance.
There is also a third strategy using an Object
as argument for the method where you want to use the array or Iterable
and it would require type checks to figure out which type the argument has, however I wouldn't recommend it at all as you usually need to consider that the Object hasn't always the required type and that you need seperate code for certain cases.
In conclusion, it's the fault of Java's problematic Generic Type system which doesn't allow to use primitive types as generic type which would save a lot of code by using simply Arrays.asList(T...)
. So you need to program for each primitive type array, you need, such a method (which basically makes no difference to the memory used by a C++ program which would create for each used type argument a seperate method.