my dog learned polymorphism*
The moose likes Beginning Java and the fly likes collections doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "collections doubt" Watch "collections doubt" New topic
Author

collections doubt

mansi gupte
Ranch Hand

Joined: Dec 30, 2008
Posts: 72
hi all,

when i try to compile the following program



i get the error as below

javac Testarraylist.java -Xlint
Testarraylist.java:8: incompatible types
found : ArrayList
required: java.util.List
List test = new ArrayList();

^
Testarraylist.java:10: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add("string");
^
Testarraylist.java:11: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add(s);
^
Testarraylist.java:12: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add(s+s);
^
Testarraylist.java:15: cannot find symbol
symbol : method size(java.lang.String)
location: interface java.util.List
System.out.println(test.size("hihi"));
^
.\ArrayList.java:11: warning: [serial] serializable class ArrayList has no defin
ition of serialVersionUID
public class ArrayList extends java.applet.Applet {
^
2 errors
4 warnings


If Arraylist is inherited from List class, why do i get error in the line marked in green color?please elaborate.Also, i wished to know that if the compiler issues a warning , can the code run [ as in , there are cases where the code gives error during compilation but still runs..... what happens when the warning is issued?]


Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
I did not have a problem running the code except for line 17 test.size() will not take a string.
Ehsan Rahman
Ranch Hand

Joined: Feb 16, 2009
Posts: 59

I tried it too and confirm Larry's statement.


SCJP 1.5, SCJD 1.6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

javac Testarraylist.java -Xlint
Testarraylist.java:8: incompatible types
found : ArrayList
required: java.util.List
List test = new ArrayList();
^



Do you happen to have you own ArrayList class defined somewhere?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

To confirm what Henry suggested, try this

java.util.List test = new java.util.ArrayList();


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
mansi gupte
Ranch Hand

Joined: Dec 30, 2008
Posts: 72
Henry Wong wrote:
javac Testarraylist.java -Xlint
Testarraylist.java:8: incompatible types
found : ArrayList
required: java.util.List
List test = new ArrayList();
^



Do you happen to have you own ArrayList class defined somewhere?

Henry


henry, you were right.I had indeed defined an arraylist class in the same folder , hence the error of incompatible types.


but why am i getting the error on line seveteen , also these bunch of warnings??


C:\JAVA PROGRAMS>javac Testarraylist.java -Xlint
Testarraylist.java:10: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add("string");
^
Testarraylist.java:11: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add(s);
^
Testarraylist.java:12: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add(s+s);
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

piya rai wrote:
but why am i getting the error on line seveteen , also these bunch of warnings??

C:\JAVA PROGRAMS>javac Testarraylist.java -Xlint
Testarraylist.java:10: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add("string");
^
Testarraylist.java:11: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add(s);
^
Testarraylist.java:12: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add(s+s);


I think you are talking about error in this statement

System.out.println(test.size("hihi"));

You will get error in this statement. What are you trying to do here. There is no method named size in List interface which takes a string.

As far as the warning goes, your code uses legacy non-generic code with collection classes that's why you are getting the warnings...
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
I guess you are using jdk1.5.
So when you declare the List, the jvm encourages you the declare list with Generics, since the default declaration is List<E> . So if you make your list as


the warning will be go.


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
mansi gupte
Ranch Hand

Joined: Dec 30, 2008
Posts: 72
Ankit Garg wrote:
piya rai wrote:
but why am i getting the error on line seveteen , also these bunch of warnings??

C:\JAVA PROGRAMS>javac Testarraylist.java -Xlint
Testarraylist.java:10: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add("string");
^
Testarraylist.java:11: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add(s);
^
Testarraylist.java:12: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
test.add(s+s);


I think you are talking about error in this statement

System.out.println(test.size("hihi"));

You will get error in this statement. What are you trying to do here. There is no method named size in List interface which takes a string.

As far as the warning goes, your code uses legacy non-generic code with collection classes that's why you are getting the warnings...



hi all,

there was this spate of errors in the above code.WITH help from you all , i have managed to catch hold of all these errors, my code runs now.Thanks all.


ps: ankit you were right , i checked the method in the API doc.Just to ask you, should i be really thorough with the api doc before exam, to do well?? cause there are so many method , there arguments , its not very easy to remember everything in there.Any special tips to score well, from your side?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

piya rai wrote:ps: ankit you were right , i checked the method in the API doc.Just to ask you, should i be really thorough with the api doc before exam, to do well?? cause there are so many method , there arguments , its not very easy to remember everything in there.Any special tips to score well, from your side?


Well the documentation is not meant to memorize the API methods. It is meant to know about the behavior of methods and the use of classes. Memorizing the API is not a good idea. Generally making a few test programs gives you the opportunity to know the API better and then you start to think about the API logically rather then memorizing it. Then you don't need to memorize anything. There are a few gotchas in the API which you'll notice when you make test programs. So just code a lot and you'll be fine...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: collections doubt
 
Similar Threads
Array problem
ArrayList error
KB page no 547
Unchecked Exception( -Xlint)
Doubt i a generics question