22

My code looks like this :

Vector<String> My_Vector=new Vector<String>();
String My_Array[]=new String[100];

for (int i=0;i<100;i++) My_Array[i]="Item_"+i;
......
My_Vector.addAll(My_Array);

But I got an error message, what's the right way to do it, without looping to add each item ?

Frank

Frank
  • 30,590
  • 58
  • 161
  • 244
  • Why are you using Vector? I'd prefer an ArrayList, because it's not synchronized by default. – duffymo Mar 05 '10 at 00:03
  • 2
    Don't use Vector, it is a legacy class, use ArrayList instead. And just use `My_Vector.add("Item_"+i);` instead of using a intermediate array – Christopher Oezbek Mar 05 '10 at 00:04
  • Wow. I haven't seen a Vector pulled out in at least 5 years. :) Brings me back to JDK 1.1. You probably want to go with Collections.synchronizedList(new ArrayList()). – sidereal Mar 05 '10 at 00:08
  • for vector and arrays of objects : `Vector rows = new Vector();` `Object[] labels = new Object[columnCount];` following methods works : `rows.add(labels);` – KNU Sep 30 '14 at 04:59

3 Answers3

61
Collections.addAll(myVector, myArray);

This is the preferred way to add the contents of an array into a collection (such as a vector).

https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#addAll-java.util.Collection-T...-

Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 2
    +1 for correcting my typo, and providing this "significantly faster" alternative. – polygenelubricants Mar 05 '10 at 00:13
  • The implementation of that method will loop: `for (T element : elements)`. – Pindatjuh Mar 05 '10 at 00:14
  • 2
    You are indeed correct, but you didn't say why it is is preferred. The Javadoc says: the behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations. – Dean Povey Mar 05 '10 at 00:27
  • @Dean: You are correct (that I didn't say why it was preferred): I was going for a "Fastest Gun in the West" answer, but indeed, the added performance is why it's preferred. – C. K. Young Mar 05 '10 at 00:29
20

The vector.addAll()takes a Collection in parameter. In order to convert array to Collection, you can use Arrays.asList():

My_Vector.addAll(Arrays.asList(My_Array));
Yannick Loriot
  • 7,107
  • 2
  • 33
  • 56
7
My_Vector.addAll(Arrays.asList(My_Array));

If you notice, Collection.addAll takes a Collection argument. A Java array is not a Collection, but Arrays.asList, in combination with Collection.toArray, is the "bridge between array-based and collection-based APIs".

Alternatively, for the specific purpose of adding elements from an array to a Collection, you can also use the static helper method addAll from the Collections class.

Collections.addAll(My_Vector, My_Array);
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623