-3

Possible Duplicate:
Java how to: Generic Array creation

I am using tons of ArrayLists in my program that does not need most of the functionality of Java arraylist, so basicly i want to implement it myself to get better space performance.

this is what i got so far:

public class ArrayList<E> {
    private E[] a;
    private int size=0;
    public ArrayList() {
    }
    public ArrayList(int fixedSize) {
        ***a=new E[fixedSize];***
    }

}

the compiler says i cant create a generic Array, how do i over come this?

Community
  • 1
  • 1
Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
  • if you really want to create an ArrayList, why not looking over here: http://www.docjar.com/html/api/java/util/ArrayList.java.html This will at least show you, how a proper implementation looks like and what to think of when making your own. – reggaemuffin Apr 18 '12 at 18:53

1 Answers1

2

The way we did it back before there were generics. Make an array of Objects and cast when taking them out. As you're the only one adding to the array, it should be safe, but you can add an instanceof check just in case.

Kevin
  • 53,822
  • 15
  • 101
  • 132