Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

initialize vector

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I have to run twice thru all the elements of a vector, it works the first time but not the second time. I think I have to initialize the index of the vector again, but don't now how to do this..
Please give an example if possible

Thanks

Hartge
 
Hi Hartge,
You could to it for example like this:

public class VectorElem
{
public static void main(String[] argv)
{
Vector vec = new Vector();

vec.add("foo");
vec.add("bar");
vec.add("foobar");

Enumeration e = vec.elements();

while(e.hasMoreElements())
{
System.out.println(e.nextElement());
}

e = vec.elements();
while(e.hasMoreElements())
{
System.out.println(e.nextElement());
}
}
}

Or use for-loop:
for(int i=0; i<vec.size(); i++)
{
System.out.println(vec.elementAt(i));
}


...if this was what you meant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top