0

For example, I'm trying to initialize a generic array of class Node<E>.

Node<Integer>[] nodes =(Node<Integer>[])new Node[10];

Is this a good practice? If not, what is a better way of doing this?

BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
Mit Mehta
  • 779
  • 8
  • 13
  • 1
    http://stackoverflow.com/questions/217065/cannot-create-an-array-of-linkedlists-in-java - Arrays and Generics do have some caveats in Java. I don't think that there is a best practice for it (but check the answers) - I would use a `List` like `ArrayList`, you can even [make a List of a fixed size - backed by an array](http://stackoverflow.com/a/5207205/664577). – Anthony Accioly Feb 15 '14 at 20:31

1 Answers1

0

This is the quick and dirty way to do it. There are other methods that are more "safe" so to speak. You can find them in a previously answered question here: How to create a type safe generic array in java?

Community
  • 1
  • 1
JD Davis
  • 3,517
  • 4
  • 28
  • 61