aspose file tools*
The moose likes Beginning Java and the fly likes ArrayList error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList error" Watch "ArrayList error" New topic
Author

ArrayList error

Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
The following code does not compile


On compiling with -Xlint option, the errors displayed are:

test_list.java:22: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
brands.add("green");
^
test_list.java:23: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
brands.add("magenta");
^
test_list.java:26: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
brands.add("light brown");
^
test_list.java:27: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
brands.add("light red");
^
test_list.java:32: cannot find symbol
symbol : variable brands
location: class test_list
return (brands);
^
test_list.java:32: illegal start of type
return (brands);
^

Please help.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The "unchecked" messages are warnings having to do with generics; we can ignore those for the present.

The actual error is the "cannot find symbol" error. That's happening because the variable "brands" is declared inside the "try" block, so it exists only inside that block; but your return statement is outside the try block. There are many ways you might fix this, but my recommendation here is to simply get rid of the "try" and "catch" blocks altogether -- along with the "throws Exception" declaration. An empty "catch" block is a terrible, terrible thing -- you're basically saying "if anything goes wrong, please don't even tell me about it." Then when your program doesn't work, you'll have no idea why!


[Jess in Action][AskingGoodQuestions]
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
public class test_list


i would suggest to name it TestList and use the Java naming conventions ;)


(peace)


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
I doubt the class file is getting created. By the way, I am using Java version 1.6
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Sen George wrote:I doubt the class file is getting created. By the way, I am using Java version 1.6


Not sure what this is in response to. Indeed, there will be no class file, because of the compile errors. You need to fix the issue with the scope of the "brands" variable.
Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
Thanks for pointing out the scoping issue. I fixed that and the program compiles fine now. However, I had to change the
to


Does this imply that if I were to add integers to the list variable brands, the code should be changed to:


I was under the impression that without generics, the ArrayList object would take an object of any type.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

That's the change to get rid of the warnings, yes.

The answer to your question is no, but not because you don't have the right idea; you just chose an unfortunate example. If you wanted to add java.util.Date objects, then

List<Date> brands = new ArrayList<Date>();

would be right. But Java generics don't work with primitives like "int". You can use the wrapper classes like "Integer", though:

List<Integer> brands = new ArrayList<Integer>();
/// This works because of "autoboxing"
brands.add(3);
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
You are correct that a non-generic List will take an Object or any of its subclasses (ie everything). But the idea of a List is that you should know what you are putting in it, and know what you are getting back from it. So the compiler will give a warning unless you are really strict with your generics, so you don't need to use a class cast.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList error