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)?

3
  • 1
    ArrayList maintains its elements in an array of Objects (Object[]) internally. It's not possible (without knowing in advance the type) to create generic arrays. Commented Aug 27, 2012 at 23:03
  • possible duplicate of Java how to: Generic Array creation Commented Aug 27, 2012 at 23:05
  • Are you perhaps of a C# background? Commented Aug 27, 2012 at 23:07

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

3 Comments

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 - the Java standard library source code has been available for free from day one. Well before Java was open sourced.
@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."
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;
    }

}

Comments

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]

5 Comments

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.
Redundant? I really don't think so.
You don't think that it is redundant to have to enter the class twice in the constructor? DRY my friend, DRY.
@mtmurdock use a factory method and let the compiler infer the generic type parameter.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.