0

I am looking to reinvent the wheel a little and create my own generic array-backed list class in Java similar to ArrayList. Yes I know this is silly, but it is an academic pursuit. The problem is that you cannot instantiate an array of a generic type

public class MySuperCoolList<E> {
   E[] array;

   public MySuperCoolList<E> () {
      array = new E[10]; // ERROR: cannot do this!
   }
}

Surely there must be a solution to this problem because Java's ArrayList is doing the same thing. The question is, how? How can I instantiate an array of a generic type E? And how is it done in ArrayList (if anyone knows)?

mtmurdock
  • 11,887
  • 20
  • 63
  • 102
  • 1
    `ArrayList` maintains its elements in an array of `Object`s (`Object[]`) internally. It's not possible (without knowing in advance the type) to create generic arrays. – MadProgrammer Aug 27 '12 at 23:03
  • possible duplicate of [Java how to: Generic Array creation](http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation) – Neil Aug 27 '12 at 23:05
  • Are you perhaps of a C# background? – Oskar Kjellin Aug 27 '12 at 23:07

3 Answers3

4

And how is it done in ArrayList (if anyone knows)?

It's open source. Take a look at the source code for ArrayList:

/**
 * The array buffer into which the elements of the ArrayList are stored.
 * The capacity of the ArrayList is the length of this array buffer.
 */
private transient Object[] elementData;
Mark Byers
  • 719,658
  • 164
  • 1,497
  • 1,412
  • Oh I didn't realize it was open source. Good work! So underneath the generics is a bunch of casting eh? Good fun. Thanks! – mtmurdock Aug 27 '12 at 23:07
  • @mtmurdock - the Java standard library source code has been available for free from day one. Well before Java was open sourced. – Stephen C Aug 27 '12 at 23:26
  • 1
    @mtmurdock Yes, that's really all Java generics are -- a compile-time check and then a bunch of casting. The keyword, if you want to read up more, is "erasure." – yshavit Aug 27 '12 at 23:49
0

In this case , you might want to use Array of Object Type , cause object type can accomodate everything and the code goes like,

public class MySuperCoolList<E> {
    Object[] array;

    public MySuperCoolList () {
       array = new Object[10];
    }

    public E get(int index){
       return (E) array[index];
    }

    public void put(int index,E val) {
      array[index] = val;
    }

}
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
Akash Yadav
  • 2,353
  • 18
  • 31
0
public MySuperCoolList<E>(final Class<? extends E> type) {
  array = (E[]) Arrays.newInstance(type, 10);
}

See Arrays.newInstance. This is how Arrays.copyOf works.

I've placed a PoC here.

int[] vals = (int[]) Array.newInstance(Integer.TYPE, 10);
vals[0] = 500;
System.out.println(vals);
System.out.println(vals.length);
System.out.println(Arrays.toString(vals));

As you can see, the output is as expected:

[I@fb53f6
10
[500, 0, 0, 0, 0, 0, 0, 0, 0, 0]
obataku
  • 27,809
  • 3
  • 38
  • 50
  • The problem with this is that the consumer must then pass the class as a parameter, which would be redundant. I'm trying to make a collection that works like ArrayList. – mtmurdock Aug 28 '12 at 20:53
  • Redundant? I really don't think so. – obataku Aug 28 '12 at 21:04
  • You don't think that it is redundant to have to enter the class twice in the constructor? DRY my friend, DRY. – mtmurdock Aug 29 '12 at 03:40
  • @mtmurdock use a factory method and let the compiler infer the generic type parameter. – obataku Aug 29 '12 at 03:54
  • You cannot get the class of a generic type at runtime, which means you still have to enter the class information twice at some point. Yes, your solution is functional, but you'll notice that none of the built in classes do it that way ie: ArrayList – mtmurdock Aug 29 '12 at 04:08