-5

I have this code.

import java.util.Vector;
import java.util.Enumeration;

/*Part Of My Private Code*/

private Vector clietns = new Vector();
private DataOutputStream remoteOut;

/*Part Of My Private Code*/

clients.addElement(remoteOut);

/*Part Of My Private Code*/

Enumeration e = clients.elements();
System.out.println(e + "");

This returns the hex code of Enumeration e. How could I obtain the exact number of elements (in integer).

Matthias
  • 3,582
  • 2
  • 30
  • 41
Jay Godara
  • 178
  • 4
  • 11

4 Answers4

2

With the method size() from Vector. Enumeration doesn't give you that method, since it can be used to enumerate elements even if the full number isn't known.

Next time you're wondering a thing like this, please go see the Javadocs first.

As a last tip, Vector and Enumeration are considered outdated. You should use ArrayList and Iterator instead.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
2

Use the Vector.size method. It will tell you the number of elements in the vector.

Brett Okken
  • 6,210
  • 1
  • 19
  • 25
1

I think you cannot get the number of items from an Enumeration.

Note that the only two methods of this interface are:

hasMoreElements()
E   nextElement()

So it is not intended to behave as a collection that usually knows the number of items inside them.

You should use vector size() method instead that returns an integer.

Rafael
  • 2,521
  • 2
  • 33
  • 59
  • O.K. Enumerations could be used to loop through the elemets of a vector. But not actually to get the elemets in a Vector.... – Jay Godara Jul 15 '14 at 12:20
0

Questions like this are best to do a little research before posting.

Take a look at the API page for Java and you'll find what you're looking for. (Hint, the function is going to return an int)

http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html

Greg Hilston
  • 2,397
  • 2
  • 24
  • 35