Vectors and accessing elementCount - Newbie Question

T

Taria

Hello all,

I just discovered the Vector class today and am having trouble
accessing the variable elementCount. First I received a protected
package error so I adjusted for this by creating another class that
extends Vector..

When I try to compile below, the error I'm encountering is:
"MyProg.java:9: warning: [serial] serializable class myVector has no
definition of serialVersionUID
public class myVector extends Vector"

import java.util.*;

public class MyProg1 {
public static void main(String[] args) {
Vector testVector = new Vector();
testVector.addElement(new String ("I am a string"));
//System.out.println ( testVector.elementCount ); //protected
package error, cannot use
}
}

public class myVector extends Vector{
public void myVector(){
Vector v = new Vector();
System.out.println (this.elementCount);
}
}

Any advice is appreaciated,
-t
 
L

Lew

Taria said:
Hello all,

I just discovered the Vector class today and am having trouble

Do you mean java.util.Vector? If so, you'll be much, much better off starting
with ArrayList as your first List implementor. Please hold off on Vector, a
very old, pre-Collection class, until you know about multi-threaded programming.
accessing the variable elementCount. First I received a protected
package error so I adjusted for this by creating another class that
extends Vector..

This is an antipattern.
When I try to compile below, the error I'm encountering is:
"MyProg.java:9: warning: [serial] serializable class myVector has no
definition of serialVersionUID
public class myVector extends Vector"

This has to do with building a correct implementation of java.io.Serializable.
Let's defer that for a moment. It is nothing to do with access to the
protected variable, something else you should not do.

You will be much, much better off either extending (yecch) ArrayList, or
extending AbstractList, or building your own version of a List altogether.

List provides the method size() that you should use in preference to any
direct access to a member variable.

However, you should not extend List, you should include a List as a member
variable of your custom class.

Besides being way too old, Vector has the feature that all its methods are
synchronized. Usually this is overhead you do not want, because you only need
thread-local access to your List. Even if you do need to synchronize access,
you can use one of the prebuilt concurrent structures, or make a
Collections.synchronizedCollection( yourList ).
<http://java.sun.com/javase/6/docs/a...#synchronizedCollection(java.util.Collection)>

Now, about that java.io.Serializable implementation.
<http://java.sun.com/javase/6/docs/api/java/io/Serializable.html>
Read and study this link, and the related material in Joshua Bloch's book,
/Effective Java/.

If you make a class implement java.io.Serializable you pretty much should
have, but the language does not require, a member variable:

ANY-ACCESS-MODIFIER static final long serialVersionUID

where "ANY-ACCESS-MODIFIER" can be any access modifier, including no access
modifier (package-private). That's what the error message told you was missing.

Implementing java.io.Serializable is a huge responsibility. It commits the
class maintainer permanently to details of an implementation, and to an
additional exposed face to the class that provides a back door to the internals.
 
T

Taria

Thanks Lew,

That was quite informative. I'm sad and glad to hold off on
java.util.Vector class. I'll head my way into ArrayList and work out
what I can.

That encounter with Vector and the search for answers made me take a
nap in middle of the afternoon. :p

-t
 
R

Roedy Green

I just discovered the Vector class today and am having trouble
accessing the variable elementCount. First I received a protected
package error so I adjusted for this by creating another class that
extends Vector..

Vector has been largely replaced by ArrayList.

You don't need to get at elementCount. You can use Vector.size().
 
R

Roedy Green

That encounter with Vector and the search for answers made me take a
nap in middle of the afternoon. :p

You strike me as someone who will eventually succeed. You have
curiosity. You are respectful to those who try to help you. You are
persistent. You don't play helpless.
 
T

Taria

You strike me as someone who will eventually succeed. You have
curiosity. You are respectful to those who try to help you. You are
persistent. You don't play helpless.

--
Roedy Green Canadian Mind Products
The Java Glossaryhttp://mindprod.com

Vector has been largely replaced by ArrayList.

You don't need to get at elementCount. You can use Vector.size().

lol oh! Now I don't feel too smart. :p My debugger showed me the
structure of the Vector class and I assumed immediately that was the
name of the field.

Thank you for your kind words, Roeddy, that's encouraging, indeed.

I'm persistant because I get the greatest satisfaction when I
understand a little more esp on the objects concepts of Java, objects
has terrorized me for a long time!

I'm done with the ArrayList/objects version of the Quicksort search
for the recursion assignment. :))) I really only needed one program
done with or without objects but I wanted to try both versions and
because of that...I FINALLY understand how to write and access a
class. whee! haha i know this part is a walk in a park for you but
it's monumental for me.

Anyway..thank you again, Lew and Roeddy (to name a few), both of you
have been very informative and I feel lucky to have found you
guys. :) (I've seen posts from you guys on other boards, too. lol
you guys are everywhere)

-t
 
P

Patricia Shanahan

Taria wrote:
....
lol oh! Now I don't feel too smart. :p My debugger showed me the
structure of the Vector class and I assumed immediately that was the
name of the field.
....

Reading the API documentation is a MUCH better way of finding out how to
use a Java class than looking at it with a debugger. The API
documentation hides private implementation, but shows comments
explaining the public and protected features.

Patricia
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top