Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"List cannot be resolved to a type"

 
Dick Hammer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"List cannot be resolved to a type." is the error message I get. I copied and pasted it from the web examples. I tried a few different ones. Why am I getting that error. The web examples are very simple and don't include libraries but I looked it up and I made sure to import the libraries as so:



Neither of the two list declarations work. What am I missing? I have bunch of other code ...main etc, but everything was working fine until I tried adding that list thing. The list declarations are outside of main, below the "import" declarations....there with my other variables. Any help would be appreciated.


 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try importing java.util.List?

Edit: yes, you should declare it as a List. That makes it easier to switch to a different implementation; for instance LinkedList, CopyOnWriteArrayList, etc.
 
Dick Hammer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh I think we were both editing and posting at same time.

So Rob, I can in fact declare it as "List" and/or "ArrayList", but if I want to declare it as "List" I have to import java.util.List? And declaring it as "List" is better right?

Internet searches did not mention that library. Looked it up in my Deitel book as far as declaring it as ArrayList though.

List <Integer> intlist = new ArrayList<Integer>();

should be

ArrayList<Integer> intlist = new ArrayList<Integer>();

But I will try that library Rob since it's better. Thanks Rob.


 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dick Hammer wrote:Doh I think we were both editing and posting at same time.

We were indeed

So Rob, I can in fact declare it as "List" and/or "ArrayList", but if I want to declare it as "List" I have to import java.util.List? And declaring it as "List" is better right?

Right on both accounts. The rule of thumb is, declare as broad as possible. If you declare as ArrayList you're stuck with that choice. You only should declare as ArrayList if you need one of the methods defined in ArrayList and not in List.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dick Hammer wrote:if I want to declare it as "List" I have to import java.util.List?


Yes. If you want to use a type that is not in the same package as your class, and is not in java.lang, then you have to import it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dick Hammer wrote: . . . List <Integer> intlist = new ArrayList<Integer>();

should be

ArrayList<Integer> intlist = new ArrayList<Integer>(); . . .
No, List<Foo> myList = new ArrayList<Foo>(); is better. If you use Java7 you can use <> for the second type instead of <Foo>.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic