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
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.