Do I have to push my elements one by one? I tried something like
String[] array;
array=...
Vector<String> vector = new Vector<String>(array);
but my eclipse marks this as an error.
Vector
doesn't have a constructor that accepts an array directly.
Assuming that array
is of type String[]
, you could do
Vector<String> vector = new Vector<String>(Arrays.asList(array));
Better to use ArrayList
as it doesn't have the overhead of having synchronized methods. You could use
List<String> list = new ArrayList<>(Arrays.asList(array));
This will produce a mutable collection also.
That can't work, since, as the documentation shows, there is no Vector constructor taking an array as argument.
If you just want a non-modifiable list, use
List<String> list = Arrays.asList(array);
If you really want a Vector (but you should use ArrayList instead, because Vector is obsolete), use
Vector<String> vector = new Vector<String>(Arrays.asList(array));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With