views:

104

answers:

5

which one do you prefer? i want to make a finite automata in java,. it's more efficient using vector or set???

A: 

A set is more efficient at insertions and deletions while a vector does these things slower. However, you cannot store elements at indices of your choice as you can in a vector.

Lookups are fastest in a vector, while they can be a tad bit slower in sets.

If your data is constant, you should go with a vector. If you will have frequent changes, you should go with a set.

casablanca
More efficient at doing *what*?
GregS
TreeSet is in natural order of the elements.
Steve Kuo
My bad, I've clarified what I actually meant.
casablanca
+2  A: 

A Vector is a class. A Set is an interface. I would use an ArrayList instead of a Vector anyway, if you're not doing something that needs to be threadsafe. Or a standard array if it's not going to get resized.

It really depends on your application though. Specifically, Sets don't allow duplicate elements, whereas Arrays (Arraylists, Vectors) do.

Personally I would use an array, unless it needed to have some kind of special functionality (resizing, no duplicate elements, etc.)

zebediah49
Agree except for preferring array over ArrayList.
Steve Kuo
i use a vector cause i don't know the size of the transition. it depends on the user's input. so it will more efficient if i use vector. i don't need to determine the size of the array . . .CMIIW
gin
A: 

I'd rather use a list implementation instead of a vector. The vector is thread safe, but it carries some overhead due to that. List is not thread safe, but you can always synchronize every call to add, remove and get elements.

The set will have some odd behaviour, like casablanca said, if you are trying to get your elements in a particular order. Also, the set will act as such, and will never allow you to repeat elements in case you need to.

Both are for different kind of activities: vectors and lists are both for the same tasks, implemented different. The set will be only for such cases when you need it to function like a set.

Oso
A: 

I think that the Set is more applied to implement a Finite Automata. For instance, you should use the Set to guarantee no duplicated elements. By the Automata definition you have a set of states, a set of symbols and a set of accepted states, so a Set could fit on your requirements.

Pedro Ghilardi
+1  A: 

A Vector and a Set are two different data structures; they are not interchangeable for the same purpose.

  • A Vector contains elements in a defined order and can contain duplicates (it's a list).
  • A Set doesn't have an inherent order and cannot contain duplicates (it's a bag, in which elements are not ordered).

Which you should use totally depends on how you design and implement your application; without knowing how you did that, it's not possible to say whether you should use a Vector or a Set.

Note: class java.util.Vector is a legacy collection class, you should really use java.util.ArrayList instead.

Jesper
my lecturer says that i'd rather used vector to set,.but when i searching about the automaton material, people in generally used set in their code. i just confused when i must designed the transition. he said that my state consist of transIn, TransOut and symbol. in which my transIn and TransOut are a vector..
gin
The problem is that you try to jump immediately from the high-level question ("I want to write a finite automata program") to the low-level implementation details ("Should I use a `Vector` or a `Set`). Which you should use entirely depends on what's in the middle. First think about the algorithm you're going to use, and then think about how you would put that algorithm into code. Then think about what data structure fits best for that algorithm.
Jesper