• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList error

 
Ranch Hand
Posts: 81
IntelliJ IDE Oracle C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 81
IntelliJ IDE Oracle C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 81
IntelliJ IDE Oracle C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic