b3ta.com board
You are not logged in. Login or Signup
Home » Messageboard » XXX » Message 3066410 (Thread)

# BUGGER IT ALL!!
Dont suppose any Java programmers know how to use Vectors? if so HEEEEELP ME!!
(, Tue 20 Apr 2004, 1:23, archived)
#
(, Tue 20 Apr 2004, 1:23, archived)
# but...
you're the moon lord... you're supposed to know EVERYTHING
my world has collapsed.
(, Tue 20 Apr 2004, 1:24, archived)
# everything about moons
not java, java is evil, its like the Anti-Moon
(, Tue 20 Apr 2004, 1:25, archived)
# Use the IF statement, luke...
(, Tue 20 Apr 2004, 1:29, archived)
# cant
have to use a vector

its part of an exam
(, Tue 20 Apr 2004, 1:30, archived)
# Urm ... read the API
They're just a container class.
You can add elements ...
You can remove elements ...
You can get the Nth element ...
Think of them as resizable arrays with silly (and non orthogonal) API.
Remember to get an enumeration from them if you need to process all the elements in a non-atomic manner (ie, to make callbacks or whatnot).
Actually the semantics of Vector suck lots and some of the Java 2 list classes/interfaces are a far better bet.

The only thing I'd really warn you against is using the seraching functionality (findElement() I think). It uses the equals() method to determine a match and it calls it on the array element, not the thing you passed in. This can lead to some unexpected results (always do searching manually, it's almost always more efficient).

EDIT: Nahh, it's indexOf() and contains() that suck like that.
(, Tue 20 Apr 2004, 2:08, archived)
# a little
clicky
what's the question?
(, Tue 20 Apr 2004, 1:25, archived)
# not those vectors
in java vectors are dynamic data structures
(, Tue 20 Apr 2004, 1:27, archived)
# sounds similar to
std vector in c++, what is the question?
(, Tue 20 Apr 2004, 1:29, archived)
#
i) in your driver class create a new Vector, and use it to store the two instances of the Politican calss you created in quesation 1


the two instances are called politician1 and politician2
i tried using myVector.add(politician1); but it errored and so did myVector.addElement(blah);
(, Tue 20 Apr 2004, 1:32, archived)
#
have you called new on the vector?

Vector v;
v = new Vector();
v.addElement(politician1);
(, Tue 20 Apr 2004, 1:37, archived)
# yeah
Vector myVector = new Vector();
myVector.addElement(politician1);
(, Tue 20 Apr 2004, 1:39, archived)
# was the error message helpful?
(, Tue 20 Apr 2004, 1:44, archived)
# as with all java errors...
no
(, Tue 20 Apr 2004, 1:53, archived)
# sommit like dis?
Vector myVector = new Vector();
myVector.add(politican1);
myVector.add(politican2);

inside the Vector, they are stored as nameless Objects to get them back, u need to use casting, and assign to new
variables

Politican pol1, pol2;
pol1 = (Politican)myVector.get(0);
pol2 = (Politican)myVector.get(1);
(, Tue 20 Apr 2004, 1:45, archived)
# oh...
and u got 'import java.util.Vector;' first thing at the top of ur class file right...
(, Tue 20 Apr 2004, 1:46, archived)
# no
i stuck "import java.util.*" so it imports everything

i'll add .Vector and see if that helps
(, Tue 20 Apr 2004, 1:48, archived)
# ah
indeed it has fixed my problem, thanks :D
(, Tue 20 Apr 2004, 1:49, archived)
# tis cool
but .*; should av worked ... maybe was a typo there?

::heads back to evil Java dissertation::
(, Tue 20 Apr 2004, 1:53, archived)
# me knows a bit
on msn/aim/4rthur,
same username

/relurks
(, Tue 20 Apr 2004, 1:29, archived)
# look up at reply ^^
(, Tue 20 Apr 2004, 1:33, archived)