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?
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.
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.
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.
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.
Related
What determines whether one should be used over the other?
I used to think that the deciding factor is whether you know the size of the things you want to store but I think there might be more to it than that.
Some more differences:
First and Major difference between Array and ArrayList in Java is that Array is a fixed length data structure while ArrayList is a variable length Collection class. You can not change length of Array once created in Java but ArrayList re-size itself when gets full depending upon capacity and load factor. Since ArrayList is internally backed by Array in Java, any resize operation in ArrayList will slow down performance as it involves creating new Array and copying content from old array to new array.
Another difference between Array and ArrayList in Java is that you can not use Generics along with Array, as Array instance knows about what kind of type it can hold and throws ArrayStoreException, if you try to store type which is not convertible into type of Array. ArrayList allows you to use Generics to ensure type-safety.
One more major difference between ArrayList and Array is that, you can not store primitives in ArrayList, it can only contain Objects. While Array can contain both primitives and Objects in Java. Though Autoboxing of Java 5 may give you an impression of storing primitives in ArrayList, it actually automatically converts primitives to Object.
Java provides add() method to insert element into ArrayList and you can simply use assignment operator to store element into Array e.g. In order to store Object to specified position.
One more difference on Array vs ArrayList is that you can create instance of ArrayList without specifying size, Java will create Array List with default size but its mandatory to provide size of Array while creating either directly or indirectly by initializing Array while creating it. By the way you can also initialize ArrayList while creating it.
Use array when you know the exact size of the collection and you don't expect to add/remove elements.
Use List (ArrayList) when you don't know the exact size of the collection and you expect to alter it at some point.
If you're using Java8, there is the Stream API, which helps to significantly reduce the boilerplate code when working with collections. This is another plus for ArrayList (and all Collections and Maps).
More info:
Arrays vs ArrayList in performance
Unless speed is critical (really critical, like every microsecond counts), use ArrayList whenever possible. It's so much easier to use, and that's usually the most important thing to consider.
Generally, I use ArrayList, not arrays, because they offer a lot of several methods that are very usefull. I think you can use array if performance is very important, in very special cases.
Array is fixed, ArrayList is growable.If the number of elements is fixed, use an array
Also one of the great benefits of collection implementations is they give you a lot of flexibility. So depending on your need, you can have a List behave as an ArrayList or as a LinkedList and so on. Also if you look at the Collection API, you'd see you have methods for almost everything you'd ever need to do.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is a List vs. an ArrayList?
Ive used both of them, but im just wondering what are the pros and cons between them? What are the major differences? And which one is better to use?
Thanks.
List is an interface implemented by ArrayList class. Another well-known implementation of the List is LinkedList.
ArrayList provides constant-time random access, while LinkedList provides constant time for non-sequential access. When you declare a variable that will hold an ArrayList, consider accessing it through an interface, like this:
List<ElementType> myList = new ArrayList<ElementType>();
This will let you swap in a different implementation without disturbing the rest of your code.
List merely describes the contract of what it means to be a list. As such, it is not a concrete implementation but merely an interface. A list can be implemented in a number of ways.
In particular, you have ArrayList, which internally keeps a dynamic array for storing all the elements in order. You also have LinkedList, which stores elements as a doubly linked list i.e. a sequence of nodes which keep references to the previous and next nodes.
Vector is another List, much like an ArrayList in that its implementation is based on a dynamic array; it's, however, a relic of the older versions of Java and is guaranteed to be thread-safe by being wholly synchronized. In practice, new Vector<T>() is more-or-less equivalent to Collections.synchronizedList(new ArrayList<T>()).
The reason for having a List is that a list can come implemented in a number of ways. That being said, often you want to have some sort of generic behavior that can be applicable to all Lists... see polymorphism.
A List is an interface, and an ArrayList is an implementation of that interface. An ArrayList is a List, and so are LinkedLists, Stacks, Vectors, etc.
the other posters already answered the "what" part of your question. Some considerations to think about when choosing between them.
An ArrayList uses an array behind the scenes. So accessing by index can be done in constant time. Adding can also be done in constant time, if the array has been allocated with enough space. However, when the space runs out, ArrayList will allocate a larger array and copy the old array values into the new one.
A LinkedList uses nodes that are chained together. Accessing by an index can potentially require walking the entire list (linear time). Inserting only requires creating a new node and adding it at the end (which could be constant time if a tail pointer is maintained).
So "which one is better" can depend on how you are using it. Truthfully, I've never measured performance differences between the two, but it's just something to consider.
If you add elements to an array list, one by one, for example:
ArrayList alist = new ArrayList();
alist.add("A");
alist.add("B");
alist.add("C");
alist.add("D");
And then retrieve, say the third element by say, alist.get(2), am I guaranteed to retrieve the third element I added?
Second part of the question, assuming the answer to the first question is yes: When would I use an ArrayList versus a Vector and vice versa.
"Guaranteed"? That's hard to answer, because of the answer to the second part of the question.
ArrayList is not synchronized by default; Vector is.
So if you're running a multi-threaded app, your ArrayList is shared, writable state, and you haven't synchronized properly you might find that guarantee isn't so solid.
If you're running in a single thread, the code you wrote would return "C".
You should still prefer ArrayList, because synchronization is expensive and you might not always want to pay the price.
Yes, you're guaranteed to retrieve the third element you've added.
If you're only working with single-threaded code, use ArrayList. If you need to make sure that the list is thread-safe, use Vector.
Yes, elements are guaranteed to be indexed in the order you added them (much like a primitive array).
You would only use a Vector when absolutely necessary since it is deprecated.
Answer for the first part of your question is yes, you will get "C" .
you should use ArrayList when single thread is manipulating(add/remove) the ArrayList.
If two or more threads are manipulating(add/remove) you need to use Vector, as Vector is thread safe
I hope this helps
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Benefits of arrays
Hey there,
are there any reasons to prefer Arrays (MyObject[]) over ArrayLists (List<MyObject>)? The only left place to use Arrays is for primitive data types (int, boolean, etc.). However I have no reasonable explanation for this, it just makes the code a little bit slimmer.
In general I use List in order to maintain a better flexibility. But are there reasons left to use real Arrays?
I would like to know,
best regards
I prefer to use Arrays over ArrayLists whenever I know I am only going to work with a fixed number of elements. My reasons are mostly subjective, but I'm listing them here anyway:
Using Collection classes for primitives is appreciably slower since they have to use autoboxing and wrappers.
I prefer the more straightforward [] syntax for accessing elements over ArrayList's get(). This really becomes more important when I need multidimensional arrays.
ArrayLists usually allocate about twice the memory you need now in advance so that you can append items very fast. So there is wastage if you are never going to add any more items.
(Possibly related to the previous point) I think ArrayList accesses are slower than plain arrays in general. The ArrayList implementation uses an underlying array, but all accesses have to go through the get(), set(), remove(), etc. methods which means it goes through more code than a simple array access. But I have not actually tested the difference so I may be wrong.
Having said that, I think the choice actually depends on what you need it for. If you need a fixed number of elements or if you are going to use multiple dimensions, I would suggest a plain array. But if you need a simple random access list and are going to be making a lot of inserts and removals to it, it just makes a lot more sense to use an Arraylist
Generally arrays have their problems, e.g. type safety:
Integer[] ints = new Integer[10];
Number[] nums = ints; //that shouldn't be allowed
nums[3] = Double.valueOf[3.14]; //Ouch!
They don't play well with collections, either. So generelly you should prefer Collections over arrays. There are just a few things where arrays may be more convenient. As you already say primitive types would be a reason (although you could consider using collection-like libs like Trove). If the array is hidden in an object and doesn't need to change its size, it's OK to use arrays, especially if you need all performance you can get (say 3D and 4D Vectors and Matrices for 3D graphics). Another reason for using arrays may be if your API has lots of varargs methods.
BTW: There is a cute trick using an array if you need mutable variables for anonymous classes:
public void f() {
final int[] a = new int[1];
new Thread(new Runnable() {
public void run() {
while(true) {
System.out.println(a[0]++);
}
}
}).start();
}
Note that you can't do this with an int variable, as it must be final.
I think that the main difference of an array and a list is, that an array has a fixed length. Once it's full, it's full. ArrayLists have a flexible length and do use arrays to be implemented. When the arrayList is out of capacity, the data gets copied to another array with a larger capacity (that's what I was taught once).
An array can still be used, if you have your data length fixed. Because arrays are pretty primitive, they don't have much methods to call and all. The advantage of using these arrays is not so big anymore, because the arrayLists are just good wrappers for what you want in Java or any other language.
I think you can even set a fixed capacity to arraylists nowadays, so even that advantage collapses.
So is there any reason to prefer them? No probably not, but it does make sure that you have just a little more space in your memory, because of the basic functionality. The arraylist is a pretty big wrapper and has a lot of flexibility, what you do not always want.
one for you:
sorting List (via j.u.Collections) are first transformed to [], then sorted (which clones the [] once again for the merge sort) and then put back to List.
You do understand that ArrayList has a backing Object[] under the cover.
Back in the day there was a case ArrayList.get was not inlined by -client hotspot compiler but now I think that's fixed. Thus, performance issue using ArrayList compared to Object[] is not so tough, the case cast to the appropriate type still costs a few clocks (but it should be 99.99% of the times predicted by the CPU); accessing the elements of the ArrayList may cost one more cache-miss more and so (or the 1st access mostly)
So it does depend what you do w/ your code in the end.
Edit
Forgot you can have atomic access to the elements of the array (i.e. CAS stuff), one impl is j.u.c.atomic.AtomicReferenceArray. It's not the most practical ones since it doesn't allow CAS of Objec[][] but Unsafe comes to the rescue.
What are the differences between the two data structures ArrayList and Vector, and where should you use each of them?
Differences
Vectors are synchronized, ArrayLists
are not.
Data Growth Methods
Use ArrayLists if there is no specific requirement to use Vectors.
Synchronization
If multiple threads access an ArrayList concurrently then we must externally synchronize the block of code which modifies the list either structurally or simply modifies an element. Structural modification means addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification.
Collections.synchronizedList is normally used at the time of creation of the list to avoid any accidental unsynchronized access to the list.
Data growth
Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.
As the documentation says, a Vector and an ArrayList are almost equivalent. The difference is that access to a Vector is synchronized, whereas access to an ArrayList is not. What this means is that only one thread can call methods on a Vector at a time, and there's a slight overhead in acquiring the lock; if you use an ArrayList, this isn't the case. Generally, you'll want to use an ArrayList; in the single-threaded case it's a better choice, and in the multi-threaded case, you get better control over locking. Want to allow concurrent reads? Fine. Want to perform one synchronization for a batch of ten writes? Also fine. It does require a little more care on your end, but it's likely what you want. Also note that if you have an ArrayList, you can use the Collections.synchronizedList function to create a synchronized list, thus getting you the equivalent of a Vector.
Vector is a broken class that is not threadsafe, despite it being "synchronized" and is only used by students and other inexperienced programmers.
ArrayList is the go-to List implementation used by professionals and experienced programmers.
Professionals wanting a threadsafe List implementation use a CopyOnWriteArrayList.
ArrayList is newer and 20-30% faster.
If you don't need something explitly apparent in Vector, use ArrayList
There are 2 major differentiation's between Vector and ArrayList.
Vector is synchronized by default, and ArrayList is not.
Note : you can make ArrayList also synchronized by passing arraylist object to Collections.synchronizedList() method.
Synchronized means : it can be used with multiple threads with out any side effect.
ArrayLists grow by 50% of the previous size when space is not sufficient for new element, where as Vector will grow by 100% of the previous size when there is no space for new incoming element.
Other than this, there are some practical differences between them, in terms of programming effort:
To get the element at a particular location from Vector we use elementAt(int index) function. This function name is very lengthy.
In place of this in ArrayList we have get(int index) which is very
easy to remember and to use.
Similarly to replace an existing element with a new element in Vector we use setElementAt() method, which is again very lengthy and may irritate the programmer to use repeatedly. In place of this ArrayList has add(int index, object) method which is easy to use and remember.
Like this they have more programmer friendly and easy to use function names in ArrayList.
When to use which one?
Try to avoid using Vectors completely. ArrayLists can do everything what a Vector can do. More over ArrayLists are by default not synchronized. If you want, you can synchronize it when ever you need by using Collections util class.
ArrayList has easy to remember and use function names.
Note : even though arraylist grows by 100%, you can avoid this by ensurecapacity() method to make sure that you are allocating sufficient memory at the initial stages itself.
Hope it helps.
ArrayList and Vector both implements List interface and maintains insertion order.But there are many differences between ArrayList and Vector classes...
ArrayList -
ArrayList is not synchronized.
ArrayList increments 50% of current array size if number of element exceeds from its capacity.
ArrayList is not a legacy class, it is introduced in JDK 1.2.
ArrayList is fast because it is non-synchronized.
ArrayList uses Iterator interface to traverse the elements.
Vector -
Vector is synchronized.
Vector increments 100% means doubles the array size if total number of element exceeds than its capacity.
Vector is a legacy class.
Vector is slow because it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.
Vector uses Enumeration interface to traverse the elements. But it can use Iterator also.
See Also : https://www.javatpoint.com/difference-between-arraylist-and-vector
Basically both ArrayList and Vector both uses internal Object Array.
ArrayList: The ArrayList class extends AbstractList and implements the List interface and RandomAccess (marker interface). ArrayList supports dynamic arrays that can grow as needed. It gives us first iteration over elements.
ArrayList uses internal Object Array; they are created with an default initial size of 10. When this size is exceeded, the collection is automatically increases to half of the default size that is 15.
Vector: Vector is similar to ArrayList but the differences are, it is synchronized and its default initial size is 10 and when the size exceeds its size increases to double of the original size that means the new size will be 20. Vector is the only class other than ArrayList to implement RandomAccess. Vector is having four constructors out of that one takes two parameters Vector(int initialCapacity, int capacityIncrement) capacityIncrement is the amount by which the capacity is increased when the vector overflows, so it have more control over the load factor.
Some other differences are: