Link to home
Start Free TrialLog in
Avatar of Suppai
Suppai

asked on

Does a Java Conversion method from ListModel (DefaultListModel) to Vector exist?

I was wondering if a methods as mentioned in subject exist in the Java API? It is not that I can't create a loop that does the trick pretty easily, but I like to use the Java API as much as possible.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

use toArray()

Vector v = new Vector(listmodel.toArray());

no and even you can't convert them!
sorry that should be:

Vector v = new Vector(Arrays.asList(listmodel.toArray()));

or if you just want a List

List list = Arrays.asList(listmodel.toArray());

Avatar of Suppai
Suppai

ASKER

I actually found out about the v.copyInto(dlm.toArray()), which is mainly the same concept. However if supposedly the dlm contains objects that are not Strings, what will happen? Is the toString method just used or?
does not matter what type if objects are in the list

SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Suppai

ASKER

Ahh I didnt make myself clear I want it to be converted to a Vector<String> and then it seems to matter, at least my editor thinks so. I want to make sure that the vector contains only Strings in order to avoid exceptions later on in the application.
if a Vector parameterezied with String, it can't take Object!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Easy enough write though, would just be a simple loop.
Let me know if you need a hand :)

With a utility method you'd get the added advantage that it would work with *any* ListModel.
So if you wanted a Vector specifically for some reason, you could just do
Vector v = new Vector(collectionsNiceListModel);

Open in new window

Avatar of Suppai

ASKER

Nice class, but maybe a little overkill for my school project:-) Yes an utility function would be good, I guess it is just a matter if identifying the class of the elements one by one in the loop and taking action if theyre not strings. However ended up realizing that i had taken care of the String checking otherwhere in the application so thorough handling of non-Strings was never necessary...but of course it is always good code to handle everything to increase re-usability I know:-)
:-)
Using a custom list model is unnecessary and just adds another (messy) class to maintain.
Not to mention that all that code does is what the simple loop I described would do just with a lot less code. It doesn't do anything that the existing model classes don't already do.