How best to do this...

I

Ike

I have a Vector() class....

I want to make it so that I will only have one instance of a given variable.

So, if my Vector class contains, say, Strings only......I want to make sure
that I don't have any dupe strings.......what ought I do? Just thought I
would ask here to get other's feedback. Thanks, Ike
 
I

Ike

But I want to maintain the order of the tems in the ArrayList....I dont
think a Map does that. -Ike
 
P

Paul Lutus

Ike said:
I have a Vector() class....

I want to make it so that I will only have one instance of a given
variable.

So, if my Vector class contains, say, Strings only......I want to make
sure that I don't have any dupe strings.......what ought I do?

An easy way, if you are limiting yourself to Vector, is to look at all the
entries in the Vector to see if there is already a match to a string about
to be inserted. If the string is present, don't insert another.
 
P

Paul Lutus

Ike said:
But I want to maintain the order of the tems in the ArrayList....I dont
think a Map does that. -Ike

So why not read the documentation to see if it, or a related class, does?
Hint: TreeMap.

Do not top-post.
 
S

Sudsy

Ike said:
But I want to maintain the order of the tems in the ArrayList....I dont
think a Map does that. -Ike

Um, didn't you say Vector in your first post? Why not just write your
own class which extends Vector and invokes the contains method in the
add method?
 
T

Tim Jowers

Ike said:
I have a Vector() class....

I want to make it so that I will only have one instance of a given variable.

So, if my Vector class contains, say, Strings only......I want to make sure
that I don't have any dupe strings.......what ought I do? Just thought I
would ask here to get other's feedback. Thanks, Ike

You mean an instance of that type.

The idea to over-ride Vector is probably right in Java. Something like:

class TypeSafeVector extends Vector
{ Class allowed;
public TypeSafeVector(Class typeAllowed)
{ allowed = typeAllowed;
}
//something like this?
Object set(Object o )
{ if( allowed.getName() == o.getClass().getName() )
return super.set(o);
else barf;
}
}
 
P

P.Hill

Tim said:
{ if( allowed.getName() == o.getClass().getName() )

Hmm, this solution doesn't do subclasses and the instanceof doesn't do dynamic
class specification.

-Paul
 
M

Michael Borgwardt

Paul said:
An easy way, if you are limiting yourself to Vector, is to look at all the
entries in the Vector to see if there is already a match to a string about
to be inserted.

Or, for short, use the contains() method.
 
M

Michael Borgwardt

Ike said:
I have a Vector() class....

Vector is obsolescent and should almost always be replaced with ArrayList.
If it is appropriate in the first place, that is.
I want to make it so that I will only have one instance of a given variable.

Then you do NOT want a List, you want a Set.

If you also want to preserve the order in which the elements were inserted, then
LinkedHashSet is exactly what you want. Only if you want to be able to insert
new elements in the middle of the implied list (or replace elements) do you
actually need a List. And then, if performance is a concern, you should still
use a Set in parallel to avoid wasting time on the test whether an element is
already present.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top