4

I use the following code try to create an array of string vectors, I hope to have an array of 3 items, each item is a string vector :

Vector<String> Result_Vector_Array[]=new Vector<String>[3];

But NB highlighted the line as error(generic array creation), what's wrong ? What's the correct way to do it ? I know there is also Arraylist, but it's not synchronized, so I want to use vector.

5
  • What is a string vector? Commented Jul 10, 2009 at 20:21
  • Isn't vector deprecated? Commented Jul 10, 2009 at 20:23
  • P.S. Use ArrayList instead!!! Commented Jul 10, 2009 at 20:23
  • Start using resultVectorArray name instead!!!! ;) Commented Jul 10, 2009 at 20:43
  • Vector is not deprecated. Very little is deprecated. Commented Jul 10, 2009 at 21:32

7 Answers 7

6

Due to type erasure, the JVM doesn't know at runtime that you have a Vector of String. The best it can do is create a 'raw' Vector. It can't guarantee for you that all Vectors actually contain Strings. That's why you get a warning from your IDE.

One way to work around this, it cast it, as jgubby suggests. Another is to put a List into your Vectors, instead of an array.

But, more importantly, why can the array have only 3 items? Wouldn't it be better to create a class with three fields to put into your Vector? With three items, that's not too much work, and you get the added bonus that you can give each of the three elements a helpful name, which should make your code a lot clearer.

Also, since Java 6, there exist a number of useful new synchronized List implementations, which might perform better than Vector, such as CopyOnWriteArrayList, or wrap a regular List in a Collections.synchronizedList.

Sign up to request clarification or add additional context in comments.

1 Comment

Create a class ! Yes, great idea, that opens a door to a lot more things, thanks !
5

You cannot create an array like that, do this:

Vector<String> Result_Vector_Array[] = (Vector<String>[]) new Vector[3];

I would suggest a different approach - arrays of containers like that are often quite hard to use, and don't help in the understanding of your code.

PS Also worth noting that the java naming convention would be

 Vector<String> resultVectorArray[] = (Vector<String>[]) new Vector[3];

and it's not usual to include the type in the name (I suspect this will be contentious!), why not just call it 'result' and let the type system worry about the type?

2 Comments

I don't understand why. Where's the generic type? String is a concrete class, and if we parameterize Vector, so will Vector be. Or?
Thanks, I use that approach is because I know the dimension of the array but don't know how many elements will be in the vector, the vectors will grow as they go, I won't know their sizes till the job is done. Any better solutions ?
1

I'd suggest to keep with collections, do something like

Collection<Vector<String>> resultVectorArray = new ArrayList<Vector<String>>(3);

Then you can use generics with the Constructor and practically spoken the same effect

Comments

1

You can also create:

Vector<Vector<String>> Result_Vector_Array=new Vector<Vector<String>>( );

Or you can replace Vector with some other collection.

Comments

0

using reflection it would be:

    Vector<String>[] arrayOfVectors = 
      (Vector<String>[]) java.lang.reflect.Array.newInstance(Vector.class, size);
    java.util.Arrays.fill(arrayOfVectors, new Vector<String>());
    for (Vector<String> v : arrayOfVectors) {
        System.out.println(v);
    }

alternatively you can use an ArrayList and then wrap it using Collections#synchronizedList(java.util.List)

1 Comment

because you cannot create an array as Vector<String> Result_Vector_Array[] = (Vector<String>[]) new Vector[3];
0

If you want to use an synchronized ArrayList, you could use the synchronizedList method in java.util.Collections.

ArrayList<String> a1 = new ArrayList<String>();
ArrayList<String> a2= new ArrayList<String>();
ArrayList<String> a3 = new ArrayList<String>();

ArrayList<String> array[] = (ArrayList<String>[]) new ArrayList[3];

array[0]= Collections.synchronizedList(a1);
array[1]= Collections.synchronizedList(a2);
array[2]= Collections.synchronizedList(a3);

Comments

-1

Should I understand you are going to use multithread on that array?...

If you are not, then you don't have to worry about the synchronization.

I would:

    List<List<String>> listOfLists = new ArrayList<List<String>>();

    List<String> firstVector = new ArrayList<String>();

    firstVector.add( "one" );
    firstVector.add( "two" );

    listOfLists.add(  firstVector );

    System.out.println( listOfLists.get(0).get(0) == "one" );
    System.out.println( listOfLists.get(0).get(1) == "two" );

Prints true, true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.