views:

350

answers:

4

I have been using vectors in the past and am very familiar with them. I have heard that ArrayLists are faster and more flexible. I am new to using ArrayLists and Java Generics. I am currently using them as follows and receiving a warning about parametrization (Which I thought I did by declaring them <String>.

ArrayList<String> arrayList = new ArrayList <String> ();
...
//then within a method
arrayList.add(file.getName()); //<-This line triggers the warning.

I have found online that this can be avoided by suppressing warnings in the method, as you can see below. However, this is a work around and I would rather learn how arraylists work and use them properly rather than suppress warnings from using them incorrectly.

@SuppressWarnings("unchecked")

And please, if there is some sort of convention or standards regarding using ArrayLists I would love to learn about those too.

EDIT: The warning I am recieving is the following: Type safety: The method add(Object) belongs to the raw type ArrayList. References to generic type ArrayList should be parameterized

+4  A: 

Firstly, refer to your list (as a variable, return type or parameter type) by the interface name so:

List<String> list = new ArrayList<String>();

It's bad form to tie your code to a particular implementation.

There is nothing inherently wrong with Vectors. They're basically ArrayLists where all the methods are synchronized. This can create unnecessary overhead but not overly so.

As for suppressing unchecked warnings, that should only be necessary if you're referring to a List with no generic argument. For example:

List list = new ArrayList<String>();
list.add("boo");

So always include an appropriate generic type whenever you use a List as a return type, variable type or parameter type.

Certain libraries may not give you that option but those should be the only circumstances in which you need to suppress unchecked warnings.

cletus
I need to see a more complete code snippet to tell you why you're getting that warning. Either you're passing an object in that isn't of the generic type or you're using a naked generic type (ie "List list" not "List<String> list").
cletus
+1  A: 

What is the warning? It seems fine to me although you should really code to its interface, List.

fastcodejava
+3  A: 

Perhaps you are passing the arrayList to a method where the method signature is void method(ArrayList arrayList) or better void method(List arrayList) and the compiler trigger unchecked warning. You should change the method signature to void method(ArrayList<String> arrayList) or better void method(List<String> list)

Chandra Patni
I think you mean: "You should change the method signature to `method(ArrayList<String> arrayList)`" and in any case, what you should really change it to, as others have said, is `method(List<String> list)`
MatrixFrog
You mean `void method(ArrayList<String> arrayList)`, or better `void method(List<String> strs)`
Tom Hawtin - tackline
(Seems that the generic argument was in the answer, but the syntax highlighting interpreted as HTML or otherwise ignored it. Edited the answer with `backticks`.)
Tom Hawtin - tackline
A: 
sateesh
Then the only possiblity I can think of is that the delcaration of variable "arrayList" in your method is something different than the one you have shown here. Is it possible that there is a different declartion of variable by same name in the method and this masks the declaration you have shown.
sateesh