2

Say I have some lists:

List<Short> shortList;
List<Integer> integerList;
List<Long> longList;
List<Float> floatList;
List<Double> doubleList;
List<Boolean> booleanList;

And I want to convert them into their primitive equivalent:

short[] shortArray;
int[] intArray;
long[] longArray;
float[] floatArray;
double[] doubleArray;
boolean[] booleanArray;

I want to make a method to that accepts any type of List of boxed primitives and return the equivalent array of unboxed primitives. I am not up to speed at Generics, so I took a shot and it doesn't work. This is what I have:

public static E[] convertObjectListToPrimitiveArray(List<T> list)
{
    E[] primitiveArray = new E[list.size()];
    int i=0;
    for(T object : list){
        E[i++] = object;
    }
    return primitiveArray;
}

What do I need to do to make this method work?

Brian
  • 1,726
  • 2
  • 24
  • 62
  • 1
    @SotiriosDelimanolis Very nice catch -- an exact match of OP's task! It's odd that it is not very popular - in four and a half years it got only a thousand hits or so. I'd expect the view count to be much higher. – Sergey Kalinichenko Jan 08 '19 at 16:33
  • 1
    @dasblinkenlight Agreed ... I did, what I would call, a thorough search through Google and never found that post. – Brian Jan 08 '19 at 16:53
  • Having duplicates like this helps build search presence, so it's all good. – Radiodef Jan 08 '19 at 18:05

0 Answers0