This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
One option is to not use arrays at all, and use List<MyType> instead. Another, if you really really need arrays of the appropriate type, is to have your instance keep a reference to the Class it's associated with (require it in the constructor or factory method). Then use java.lang.reflect.Array.newInstance(clss, size) to create the appropriate array.
could someone suggest what a solution for the following code would be?
at the moment i have to up cast everything to Object as the above is illegal:
this creates a problem as if i wanted to iterate through the array using the new syntax:
You have found (one of many limitations) a limitation of generics. You'll find evidence of your best case scenario in places such as the source to java.util.ArrayList or net.tmorris.adt.set.MutableSet, which amounts to a compile-time warning (or alternatively, something other than an array without O(1) seek).