Java J2ME JSP J2EE Servlet Android

Java Vector to array or List to array

We can easily get an array from Vector or List by just calling toArray() methods. This will generate arrays of Objects. Here is an example of getting array of Strings from java.util.Vector and java.util.List. I hope this will be helpful to you..

Vector friends = new Vector();
friends.add("Tuhin");
friends.add("Sumon");
friends.add("Habib");
friends.add("Shishir");
friends.add("Kiser");
friends.add("Zimi");
friends.add("Lizzie");

String arr[] = (String[]) friends.toArray(new String[0]);
System.out.println(Arrays.toString(arr));

List friendsList = new ArrayList();
friendsList.add("Tuhin");
friendsList.add("Sumon");
friendsList.add("Habib");
friendsList.add("Shishir");
friendsList.add("Kiser");
friendsList.add("Zimi");
friendsList.add("Lizzie");
arr = (String[]) friendsList.toArray(new String[0]);
System.out.println(Arrays.toString(arr));

If you execute the above code block. This will generate an output as follows..

[Tuhin, Sumon, Habib, Shishir, Kiser, Zimi, Lizzie]
[Tuhin, Sumon, Habib, Shishir, Kiser, Zimi, Lizzie]