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.

Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
user3392464
  • 141
  • 9
  • 1
    Is there a specific need to user `Vector`? http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – Rey Libutan Mar 18 '14 at 05:00
  • not as such. All I need is dynamic array, is there a way around with Lists? – user3392464 Mar 18 '14 at 05: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. – trutheality Mar 18 '14 at 05: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? – trutheality Mar 18 '14 at 05:08

3 Answers3

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.

Ted Hopp
  • 222,293
  • 47
  • 371
  • 489
  • NOTE: Compiler doesn't give warning, but an error for generic type array creation. – Rohit Jain Mar 18 '14 at 05:06
  • @RohitJain - Yeah, you're right. I got rid of that bit of misdirection. Thanks for the edit, as well. – Ted Hopp Mar 18 '14 at 05:09
  • Do I need to change the return type too to `List>`? and what is advantage of using `List` (and `List>`) instead of `Vector`? – user3392464 Mar 18 '14 at 05:13
  • 1
    @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. – Ted Hopp Mar 18 '14 at 05:15
  • 1
    @user3392464 see http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated?lq=1 – trutheality Mar 18 '14 at 05:16
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.

Community
  • 1
  • 1
Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
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)

Shriram
  • 4,124
  • 7
  • 28
  • 58