I have a question regarding the differences of the following codes:
Vector v = new Vector();
String [] str_arr = new String[3];
for(int i=0; i<3; i++)
{
str_arr[0] = "A";
str_arr[1] = "B";
str_arr[2] = "C";
v.add(str_arr);
}
System.out.println(v.size()); //answer 3
Versus
Vector v = new Vector();
for(int i=0; i<3; i++)
{
String [] str_arr = new String[3];
str_arr[0] = "A";
str_arr[1] = "B";
str_arr[2] = "C";
v.add(str_arr);
}
System.out.println(v.size()); //answer 3
The only difference between both codes is, for the second one, string array is created inside the loop.
Both the codes produce same result, but i want to know what is the difference between these two.
Vector, it isn't useful in 99% cases. It's a deprecated collectionVectoris synchronized (at least since Java 1.5).ArrayListis not. You are correct that is not deprecated, but its use is discouraged when thread safety is not an issue.