The Artima Developer Community
Sponsored Link

Java Answers Forum
Printing a vector

3 replies on 1 page. Most recent reply: Oct 11, 2002 7:48 AM by patrickl

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Printing a vector Posted: Oct 10, 2002 3:30 PM
Reply to this message Reply
Advertisement
I have these objects called pairs that consist of two Strings, and I add them to a vector, I just want to know how to print the vector out so that I can see all of the pairs in the vector. Can you guys help me?


Julio C. Fonseca C.

Posts: 18
Nickname: jfonseca
Registered: Mar, 2002

Re: Printing a vector Posted: Oct 10, 2002 7:09 PM
Reply to this message Reply
Hi,

I just would loop through the elements of the vector, make a casting and the print out each one of the objects via a printing mechanism of its class.

I mean something like

class MyClass
{
String a, b;

void Show()
{
System.out.println(a + " " + b);
}

}

class AClass
{
Vector v; //this class contains MyClass objects
void Show ()
{
for (int i = 0; i < v.size(); i++)
((MyClass)(v.elementAt(i))).Show();
}
}



Hope it helps,
Julio

Ian Phillips

Posts: 8
Nickname: ianp
Registered: Oct, 2002

Re: Printing a vector Posted: Oct 11, 2002 1:24 AM
Reply to this message Reply
Or more generally:

for (Iterator i = v.iterator(); i.hasNext();) {
  ((MyClass)v.next()).show();
}


Since this would work efficiently for any java.util.Collection.

Ian.

patrickl

Posts: 2
Nickname: patrickl
Registered: Oct, 2002

Re: Printing a vector Posted: Oct 11, 2002 7:48 AM
Reply to this message Reply
Just implement the toString() method in your Pair class.
Then print the vector. No need for iteration.

Flat View: This topic has 3 replies on 1 page
Topic: Invalid authorization specification: Access denied for user Previous Topic   Next Topic Topic: problem with this function

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use