File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question in List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question in List " Watch "Question in List " New topic
Author

Question in List

Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208

public class Erode <X,Y,Z>
{
public static void main( String args[] )
{
List <?> list = new LinkedList<Erode<Integer, String, Long>>();
}
}

This program compiles very Fine BUT when i try to ADD something in the list
FOR EG list.add(5);

it Gives me an Error...PLEASE HELP.....


OCJP-90%,OCPWCD-95%
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

What is the error? And do you think 5 is Erode? where as the list expects Erode instances


Mohamed Sanaulla | My Blog
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

List<?> is a unbounded type. is known as List of unknown type . so you cant add any type into it other than null.

change the line as
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208




Hello Seetharaman you are right that an ? is unbounded type but i have seen many instances in the book and the exam like
FOR EG if i declare this:
List <?>l =new LinkedList<Integer>
then this List gets defined to take INTEGERS ......m i right ??.....
...
So in ma prev eg if i had declared
List <?> list = new LinkedList<Erode<Integer, String, Long>>();
the list should be of take an ERODE instance right ??....but even when i TRY to ADD and ERODE Instance to it it Gives me an Compiler Error like this

cannot find symbol
symbol :method add (Erode)
location: interface java util.List <capture of ?>
list.add(new Erode());

^

Please SOMEONE HELP ME
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208

sagar shroff wrote:


Hello Seetharaman you are right that an ? is unbounded type but i have seen many instances in the book and the exam like
FOR EG if i declare this:
List <?>l =new LinkedList<Integer>
then this List gets defined to take INTEGERS ......m i right ??.....
...
So in ma prev eg if i had declared
List <?> list = new LinkedList<Erode<Integer, String, Long>>();
the list should be of take an ERODE instance right ??....but even when i TRY to ADD and ERODE Instance to it it Gives me an Compiler Error like this

cannot find symbol
symbol :method add (Erode)
location: interface java util.List <capture of ?>
list.add(new Erode());

^

Please SOMEONE HELP ME





Hey Guys thank you actually i was Mixing 2 things ...!!!.......i GOT it when you declare <?> ...you simply cannot add any thing...
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
following code should work in your case -

for more detail of how and why, please go through the following link -
Capture of ?" Compile error, what does it mean?


Regards
Shalindra Singh
SCJP, SCWCD
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208

shalindra Singh Suryvanshee wrote:following code should work in your case -

for more detail of how and why, please go through the following link -
Capture of ?" Compile error, what does it mean?



Thanks A lot !!! shalindra
 
Consider Paul's rocket mass heater.
 
subject: Question in List