• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector conversion problem  RSS feed

 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm still a little dicey on the subject of generics. Since generics was introduced with version 5 of JDK I need to understand how to change the following:

Vector results = new Vector();
results.addAll(getSQL().getData("results_n", "elementid,response", filter).toVector());
results.addAll(getSQL().getData("results_s", "elementid,response", filter).toVector());

In this code, it appears Vector results is loading up other vectors. When a vector is loading other vectors, what should the declaration be?

I tried Vector<Vector> results = new Vector<Vector>();

But when I compile I get:

DetailList.java:26: warning: [unchecked] unchecked conversion
found : java.util.Vector
required: java.util.Collection<? extends java.util.Vector>
results.addAll(getSQL().getData("results_n", "elementid,response", filter).toVector());

Please advise,

Alan
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried Vector<Vector> results = new Vector<Vector>();


That's almost correct, but you've missed off the generic type of the nested Vector inside the outer Vector! For example, if you don't care about what things your inner Vector contains, you could use:

Vector<Vector<?>> results = new Vector<Vector<?>>();

Alternatively, if you know the inner Vector contains all ResultSets for instance, then:

Vector<Vector<ResultSet>> results = new Vector<Vector<ResultSet>>();

would be good. The reason you get the compilation error is because your inner Vector type (not the outer one) is not fully specified.

However, I don't believe this is what you're trying to achieve here. Instead, I believe you want to declare something similar to:

and where the toVector() method returns a Vector<ResultSet> (i.e. a Vector containing ResultSets) - you might obviously want to replace ResultSet with the actual data type returned. Now you have a load of ResultSets inside a single Vector...

It is difficult to advise you further without also seeing at least the signatures for the getSQL(), getData() and toVector() methods. Some implementation code might also help when advising where to put generics in other places.
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

Ok, I've traced the following code:

results.addAll(getSQL().getData("results_n", "elementid,response", filter).toVector());

This part:

getSQL().getData("results_n", "elementid,response", filter)

returns a class of type SQLTable. It is a wrapper for a Vector of type String[] (declared like so: Vector<String[]> rows = new Vector<String[]>() ;) . SQLTable has a method called toVector(). It returns the vector of type String[].

Now, I tried redeclaring the vector we were discussing earlier to:

Vector<Vector<String[]>> results = new Vector<Vector<String[]>>();

The compiler didn't like it either as you can see:

DetailList.java:26: warning: [unchecked] unchecked conversion
found : java.util.Vector
required: java.util.Collection<? extends java.util.Vector<java.lang.String[]>>
results.addAll(getSQL().getData("results_n", "elementid,response", filter).toVector());

So I tried:

Vector<Vector<?>> results = new Vector<Vector<?>>();

Again the compiler complains:

DetailList.java:26: warning: [unchecked] unchecked conversion
found : java.util.Vector
required: java.util.Collection<? extends java.util.Vector<?>>
results.addAll(getSQL().getData("results_n", "elementid,response", filter).toVector());

I'm lost. Please help.

Alan
[ May 07, 2006: Message edited by: Alan Shiers ]
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your toVector() method return Vector<String[]>?

If so, then try the following:



This should work, but toVector() must return the generics version Vector<String[]> and not just Vector (which is what the compilation error is suggesting to me).

Note the method header of addAll() on Vector<E> (i.e. a Vector of type E) is:

So you must pass a Collection subclass of a generic type E or a descendent into this method. Now in your case, we've just declared Vector<String[]> and so E is actually String[] here. The method in our case becomes:

So provided we pass some Collection subclass which is declared to have elements of type String[] (or subclass) into the addAll() method, everything should work okay.

You don't want to declare Vector<Vector<...>> anything because addAll() doesn't add a Vector to your Vector - it adds all the, in your case String[], elements to your existing Vector. 'results' contains String[] arrays, not Vectors.

Does that help?
[ May 07, 2006: Message edited by: Charles Lyons ]
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that was it!
It was the pesky return declaration on the toVector() method.

Originally, it was:
public Vector toVector()
{
return rows;
}

So I changed it to:
public Vector<String[]> toVector()
{
return rows;
}

Everything works great now. Thanks!

Alan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!