wood burning stoves*
The moose likes Beginning Java and the fly likes 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 ""List cannot be resolved to a type"" Watch ""List cannot be resolved to a type"" New topic
Author

"List cannot be resolved to a type"

Dick Hammer
Greenhorn

Joined: Jan 28, 2012
Posts: 18
"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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dick Hammer
Greenhorn

Joined: Jan 28, 2012
Posts: 18
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 37953
    
  22
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>.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "List cannot be resolved to a type"
 
Similar Threads
LinkedList problem
Generics Question from K&B Study Guide.
Tough Generics question from S&B 5.0 book
[SCJP] Generics questions form SCJP 5 study Guide
Most specific method.......