1

First I should say that in my book (2005), Vector<E> is (extensively used) in place of arrays. At the same time there is no explanation with differences between the two. Checking the Oracle Doc for Vector class it's pretty easy to understand its usage.

Doing some additional research on StackOverflow and Google, I found that the Vector class is actually deprecated and to use ArrayList instead, is this correct? I also found an extensive explanation about differences between Array and ArrayList.

The part that I can't really understand: Is there a rule on where I should use ArrayList instead of simple arrays? It seems like I should always use ArrayList. It looks more efficient and should be easier to implement collections of values/objects, is there any down side with this approach?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
jnardiello
  • 699
  • 8
  • 15
  • 2
    `Vector` vs `ArrayList`: [Why is Java Vector class considered obsolete or deprecated?](http://stackoverflow.com/q/1386275/1065197). Arrays vs `List` backed by `ArrayList`: it will depend on your design, but I would prefer `List` for being dynamic and already handle all the operations (add, remove, get) instead of rewriting them, also you can use `Array#asList` to move an array to `List` and `List#toArray` for vice versa. – Luiggi Mendoza Jul 13 '13 at 18:54
  • 1
    There is no such thing as `Array`, except `java.lang.reflect.Array` which is a utility class for array manipulations. Do not mix this and `ArrayList` which is an implementation of the `List` interface. – fge Jul 13 '13 at 18:56
  • 1
    @fge when OP refers to *Array* it means a simple array of primitives or objects like `int[]` or `Object[]`. – Luiggi Mendoza Jul 13 '13 at 18:58
  • Yep, sorry i totally mixed up. Just meant simple arrays as metioned by Luiggi. Such as String[], int[], etc.. – jnardiello Jul 13 '13 at 18:59
  • @LuiggiMendoza yes, I have kind of figured this out -- I just wanted to clear the confusion, because there actually _is_ `Array` in the JDK – fge Jul 13 '13 at 19:05

4 Answers4

4

Some history:

  • Vector exists since Java 1.0;
  • the List interface exists since Java 1.2, and so does ArrayList;
  • Vector has been retrofitted to implement the List interface at that same time;
  • Java 5, introducing generics, has been introduced in 2004 (link).

Your course, dating back 2005, should have had knowledge of ArrayList at the very list (sorry, least), and should have introduced generics too.

As to Array, there is java.lang.reflect.Array, which helps with reflections over arrays (ie, int[], etc).

Basically:

  • Vector synchronizes all operations, which is a waste in 90+% of cases;
  • if you want concurrent collections, Java 5 has introduced ConcurrentHashMap, CopyOnWriteArrayList etc, you should use those;
  • DO NOT use Vector anymore in any event; some code in the JDK still uses it, but it is for backwards compatibility reasons. In new code, there are better alternatives, as mentioned in the previous point;
  • since Java 1.2, Vector does not offer the same thread safety guarantees as it used to offer anyway.

The latter point is interesting. Prior to Iterator there was Enumeration, and Enumeration did not offer the possibility to remove elements; Iterator, however, does.

So, let us take two threads t1 and t2, a Vector, and those two threads having an Iterator over that vector. Thread t1 does:

while (it.hasNext())
    it.next();

Thread t2 does:

// remember: different iterator
if (!it.hasNext())
    it.remove();

With some unlucky timing, you have:

t1                  t2
------              ------
hasNext(): true
                    .hasNext(): false
                    removes last element
.next() --> BOOM

Therefore, Vector is in fact not thread safe. And it is even less thread safe since Java 5's introduction of the "foreach loop", which creates a "hidden" iterator.

fge
  • 119,121
  • 33
  • 254
  • 329
3

The basic difference between an array and an ArrayList is that an array has fixed size, whereas, ArrayList can dynamically grow in size as needed. So, if you are assured that your array size won't change, then you can use it. But if you want to add elements later then a an ArrayList which is an implementation of List interface, is the way to go.

Although an ArrayList is internally backed by an array only. So, internally it also uses a fixed size array, with an initial capacity of 10 (which can change for that matter), but that detail is internally hidden. So, you don't have to bother about the changing size of the ArrayList.

Whenever you add elements more than the current size of array in your ArrayList, the internal array is extended. That means, the regular expansion of size can become an overhead, if you are regular inserting a large number of elements. Although this is rarely the case. Still, you can also give your own initial size while creating an ArrayList. So, that's upto you to decide.

As for Vector vs ArrayList discussion, yes Vector is now deprecated (not technically though, but it's use is discouraged as stated in comments by @Luiggi), and you should use an ArrayList. The difference is that Vector synchronizes each operation, which is nearly never required. When you need synchronization, you can always create a synchronized list using Collections.synchronizedList.

For more on this discussion, see this post.

An ArrayList is an implementation of List. There are other variations too. Like you also have a LinkedList, to get the functionality of a traditional linked list.

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 2
    *`Vector` is now deprecated* it is not technically deprecated, but its usage is highly discouraged, similar to scriptlets for JSPs. – Luiggi Mendoza Jul 13 '13 at 19:01
  • @LuiggiMendoza. Yeah, should have added that. Thanks :) – Rohit Jain Jul 13 '13 at 19:06
  • 1
    @Downvoter Please leave a comment. Anything wrong with the post? – Rohit Jain Jul 13 '13 at 19:09
  • Not downvoter (just removed my vote uptil you updated your answer to fix the deprecated part) but guess you could use [What does it mean to “program to an interface”?](http://stackoverflow.com/q/383947/1065197) for the bottom line. – Luiggi Mendoza Jul 13 '13 at 19:13
  • @LuiggiMendoza. Well, I guess that is not related to this question though. I just mentioned about `LinkedList`, so that OP can look over the options. – Rohit Jain Jul 13 '13 at 19:16
  • In fact, `Vector` also implements `List`, so by programming to interfaces, you can easily replace `List theList = new Vector();' by `List theList = new ArrayList();'. – Luiggi Mendoza Jul 13 '13 at 21:04
0

Vector Class is actually deprecated and to use ArrayList instead, is this correct?

Yes this is correct. Vector class and some other collections are deprecated and replaced with new collections like ArrayList, Map, etc. Here are few reasons why Vector is deprecated

Is there a rule on where i should use ArrayList instead of simple Arrays?

Almost always. I can think of two reasons why you should use arrays:

  • Makes JNI calls easier. It is MUCH easier to send a simple array from C++ to Java than an object of ArrayList
  • You can gain a little bit of performance, since access to elements of simple array does not requires boundaries checks and method calls.

On other hand using ArrayList gives a lot of advantages. You do not need to think about controlling array's size when you add new element, you can use simple API of ArrayList for adding/removing elements from your collection, etc.

Community
  • 1
  • 1
Ivan Mushketyk
  • 8,107
  • 7
  • 50
  • 67
0

I'll just add my two cents.

If you need a collection of primitive data and optimization matters, arrays will always be faster, as it eliminates the requirement of auto-boxing and auto-unboxing.

Mordechai
  • 15,437
  • 2
  • 41
  • 82