Adding Vectors

I

Ike

Suppose I have a Vector of Vectors, to which I wish to add another Vector of
Vectors. Can someone show me what that looks like in code? Thanks, Ike
 
M

Michael Borgwardt

Ike said:
Suppose I have a Vector of Vectors, to which I wish to add another Vector of
Vectors. Can someone show me what that looks like in code? Thanks, Ike

You'd probably use the addAll() method, at least if by "adding" you mean that
all the contents (vectors) in the fist vector should be added to the second one.

BTW, Vector is obsolescent, one should use ArrayList instead, it's faster and
less cluttered.
 
V

VisionSet

Ike said:
Suppose I have a Vector of Vectors, to which I wish to add another Vector of
Vectors. Can someone show me what that looks like in code? Thanks, Ike

It doesn't make any difference that your Vector holds vectors, it behaves as
any other Object.

Vector v1 = new Vector();
Vector v2 = new Vector();

v1.add(new Vector());
v1.add(new Vector());
v1.add(new Vector());

v2.add(new Vector());
v2.add(new Vector());
v2.add(new Vector());

v2.addAll(v1); // append the contents of v1 to v2
 
J

John C. Bollinger

Ike said:
Suppose I have a Vector of Vectors, to which I wish to add another Vector of
Vectors. Can someone show me what that looks like in code? Thanks, Ike

If you start by defining what it means (in this homework) to add one
Vector of Vectors to another then you'll be halfway there. For
instance, should the result be a Vector that contains all the Vectors
from the two operands? Or do you need some kind of component-wise
addition of the inner Vectors? Something else?

After you know what you need to do, examine Vector's API docs to find
useful methods to help you accomplish it. Don't [in general] neglect
methods inherited from superclasses. Depending on exactly what you want
to do, it could be as simple as one line of code.

There could also be other classes that would help you. Since Vector is
a member of the Java Collections framework, you might consider
consulting the Collections trail of the Java Tutorial.


John Bollinger
(e-mail address removed)
 
I

Ike

Thanks Michael,

Yes, I never use Vector, working with someone's ancient code here, and a
little reluctant to try to convert all these Vector statements into ones
comprised of ArrayLists. I think the main difference though is that Vectors
are supposedely somehow threadsafe whereas ArrayLists are not? -Ike
 
I

Ike

God, I wish it was homework John(!). I asked here in the group more as a
consequence of self doubt, of something nasty perhaps coming up down the
line as a result of blowing a deep copy of this, and merely was hoping for
the feedback from all of your good minds. Thanks, Ike.
John C. Bollinger said:
Ike said:
Suppose I have a Vector of Vectors, to which I wish to add another Vector of
Vectors. Can someone show me what that looks like in code? Thanks, Ike

If you start by defining what it means (in this homework) to add one
Vector of Vectors to another then you'll be halfway there. For
instance, should the result be a Vector that contains all the Vectors
from the two operands? Or do you need some kind of component-wise
addition of the inner Vectors? Something else?

After you know what you need to do, examine Vector's API docs to find
useful methods to help you accomplish it. Don't [in general] neglect
methods inherited from superclasses. Depending on exactly what you want
to do, it could be as simple as one line of code.

There could also be other classes that would help you. Since Vector is
a member of the Java Collections framework, you might consider
consulting the Collections trail of the Java Tutorial.


John Bollinger
(e-mail address removed)
 
J

John C. Bollinger

Ike said:
God, I wish it was homework John(!). I asked here in the group more as a
consequence of self doubt, of something nasty perhaps coming up down the
line as a result of blowing a deep copy of this, and merely was hoping for
the feedback from all of your good minds. Thanks, Ike.

OK then, sorry. It just seemed precisely the kind of vague and abstract
question that is typical of so many homework-question posts. Plus, for
the simple case, addAll seemed too obvious.

John Bollinger
(e-mail address removed)
 
J

John C. Bollinger

I said:
OK then, sorry. It just seemed precisely the kind of vague and abstract
question that is typical of so many homework-question posts. Plus, for
the simple case, addAll seemed too obvious.

Hmmm, I'm not sure that came out right. Lest I start choking before I
can get my foot out of my mouth, let me just apologize again.


John Bollinger
(e-mail address removed)
 
M

Michael Borgwardt

Ike said:
Thanks Michael,

Yes, I never use Vector, working with someone's ancient code here, and a
little reluctant to try to convert all these Vector statements into ones
comprised of ArrayLists.

OK, that is understandable, although in a good IDE there is little reason
for the reluctance.

I think the main difference though is that Vectors
are supposedely somehow threadsafe whereas ArrayLists are not? -Ike

Indeed ArryList is not snychronized (making it faster) while Vector's methods
are synchronized. But that doesn't automatically make the class "thread safe"
at all! In fact it's probably even less safe because people hear that it's
synchronized and think they don't have to worry, while in fact an iteration
over the elements or a sequence of method calls that depend on each other's
results can still produce inconsistent data.

Thus, one should generally use ArrayList and avoid multithreaded programming,
and when one *does* have to deal with it, synchronize with the necessary
granularity after completely understanding the problem. Even if method-grained
synchronization is indeed sufficient, ArrayList can still be used by
synchronizing it via Collections.synchronizedList(), although it will be a
bit slower than Vector in that case.
 
I

Ike

Its ok, I'm grateful for your help. I find all-too-often in the past when I
assume deep copies are occuring, something vague and deep within my data
structure, isnt coming over. Such things seem to crop up at the client's
site usually around 7:30 on a Friday night,lol. Thanks Again, Ike
 
P

Paul Lutus

Ike said:
God, I wish it was homework John(!). I asked here in the group more as a
consequence of self doubt, of something nasty perhaps coming up down the
line as a result of blowing a deep copy of this, and merely was hoping for
the feedback from all of your good minds. Thanks, Ike.

But you didn't asnwer any of his questions.
 

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