Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList error

 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class test_list


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


(peace)
 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic