In Java libaray, there is Interface List<E>.
what does <E> in List<E> mean?
Thanks.
Printable View
In Java libaray, there is Interface List<E>.
what does <E> in List<E> mean?
Thanks.
It tells you the type of object that is stored in the List. For example:
Will allow you to only store String objects in the list:Code:List<String> myList = new ArrayList<String>();
Read up on Lesson: Generics (The Java™ Tutorials > Bonus) for more information.Code:list.add( "Hello" ); // this is valid
list.add( new Integer(1) ); // this is invalid
Thank you for your quick replay. Do you know
1 what ? in addAll(Collection<? extends E> c) mean?
2 what extends in addAll(Collection<? extends E> c) mean?
(extends seems to mean any data type that is subclass of E)
Thank you for your quick reply ( not replay).
? is the wildcard character, and you are correct that ? extends E means any subclass of E, or E itself.
<E> is a placeholder and stands for Element and respresents any type of object. for example you can use your own class for E or other java classes like String or Integer. a <K> stands for Key and <V> for Value. for further details read the tuturials related to generics.
1 What is the purpose of the '?' wildcard character. Could you show me what character can be used and the meaning when it replaces of '?'?
2 If E means any data type (excludes the primitives), is 'extends' redundant in addAll(Collection<? extends E> c)? It seems '? extends' must be used together.
"y extends" is only when you want to specify any class that extends E. It is possible, though, to specify "?" by itself to mean any and all classes.
ra4king must be an expert in Java.
Hehe, I try ;)