0

I want a method to return two vectors to the calling function. Here is what I tried:

static Vector<String>[] method()
{
    Vector<String>[] toret = new Vector<String>[2]; // GETTING ERROR HERE
    for(...)
    {
         toret[0].add(...);
         toret[1].add(...);
    }
    return toret;
}

public static void main()
{
    Vector<String>[] obtained = method();
}

Need help to remove that error.

4
  • 1
    Is there a specific need to user Vector? stackoverflow.com/questions/1386275/… Commented Mar 18, 2014 at 5:00
  • not as such. All I need is dynamic array, is there a way around with Lists? Commented Mar 18, 2014 at 5:02
  • Mixing generics and arrays in Java is a mess (it's not impossible, but it's a mess). If you need to use generics, use an ArrayList instead of an array. Commented Mar 18, 2014 at 5:06
  • Also, for the future, say what error you're getting, because everyone is assuming you're getting a generic array creation error, which is something you could get around, but what if you got a different error? Commented Mar 18, 2014 at 5:08

3 Answers 3

2

Don't try to create arrays of generics. Try returning a List<Vector<String>> instead.

static List<Vector<String>> method()
{
    List<Vector<String>> toret = new ArrayList<Vector<String>>();
    toRet.add(new Vector<String>());
    toRet.add(new Vector<String>());
    for(...)
    {
         toret.get(0).add(...);
         toret.get(1).add(...);
    }
    return toret;
}

I'd also suggest using List<String> (and List<List<String>>) instead of Vector<String> (and List<Vector<String>>) unless you absolutely need elsewhere the method-level synchronization that Vector provides.

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

5 Comments

NOTE: Compiler doesn't give warning, but an error for generic type array creation.
@RohitJain - Yeah, you're right. I got rid of that bit of misdirection. Thanks for the edit, as well.
Do I need to change the return type too to List<Vector<String>>? and what is advantage of using List<String> (and List<List<String>>) instead of Vector<String>?
@user3392464 - Yes, I had the return type wrong. The main advantage of avoiding a Vector is that you avoid the overhead of making synchronized calls when calling the various methods. ArrayList was introduced as an alternative to Vector primarily because it is more efficient in this way. When you do need synchronization, it is almost never at the single-method level anyway, so Vector buys you nothing but inefficiency.
1

You're doing 2 wrong things here:

  • Using Vector instead of List
  • Creating an array of parameterized type.

You can't create an array of concrete parameterized types. You have to go with a List<List<String>> rather.

Comments

0

instead of Vector[] toret = new Vector[2]; Use Vector toret = new Vector();

Also change like this

public static void main() { Vector obtained = method(); }

You can use 0 & 1 st index for your manipulations. Vector is a dynamic array. so if you want to ensure capacity just give Vector toret = new Vector(2)

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.