• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

collections doubt

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?]


 
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not have a problem running the code except for line 17 test.size() will not take a string.
 
Ranch Hand
Posts: 59
Netbeans IDE VI Editor Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it too and confirm Larry's statement.
 
author
Posts: 23836
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To confirm what Henry suggested, try this

java.util.List test = new java.util.ArrayList();
 
mansi gupte
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
mansi gupte
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!