Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of The type parameter T is hiding the type T in T[] toArray(T[] a) using Eclipse without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
Using eclipse 4.2 with Java 7 and trying to implement the following method of the List interface i got a warning.
public <T> T[] toArray(T[] a) { return a; }
The warning says :
The type parameter T is hiding the type T
Why ? How can i get rid of it ?
Answer
The List interface is also generic. Make sure that you are not also using T for the generic type in your class. Note that in http://docs.oracle.com/javase/6/docs/api/java/util/List.html, they use “E” for the class generic parameter and “T” for the toArray() generic parameter. This prevents the overlap.
public class MyList<T> implements List<T> { // V1 (compiler warning) public <T> T[] toArray(T[] array) { // in this method T refers to the generic parameter of the generic method // rather than to the generic parameter of the class. Thus we get a warning. T variable = null; // refers to the element type of the array, which may not be the element type of MyList } // V2 (no warning) public <T2> T2[] toArray(T2[] array) { T variable = null; // refers to the element type of MyList T2 variable2 = null; // refers to the element type of the array }
}
We are here to answer your question about The type parameter T is hiding the type T in T[] toArray(T[] a) using Eclipse - If you find the proper solution, please don't forgot to share this with your team members.