aspose file tools*
The moose likes Beginning Java and the fly likes ArrayList  Vector Collections Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList  Vector Collections Array" Watch "ArrayList  Vector Collections Array" New topic
Author

ArrayList Vector Collections Array

yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
I am sorry to ask this basic question but still I cant get this basic's right.Please give the difference between ArrayList,vector,collections and array


Thanks for your reply and time.<br />Windows 2000,j2sdk1.4.2_08
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Complete chapters of Java books have been written on this subject, so any answer we can give here would be a high level summary. Here is such a summary:

"Collections" is an all encompassing term. The Java Collections Framework API consists of various interfaces and classes to create and use common data structures, Lists, stacks, maps, etc. You can read more about the collections API in the Java documentation (see above link), and learn more about it in the Collections Trail of Sun's online Java Tutorial, or in Chapter 11 of the opensource (i.e. free) e-book Thinking in Java.

An ArrayList and a Vector are very similar. The Vector class has been a part of Java since the beginning (i.e. since JDK1.0). The Java Collections Framework was added in Java version 1.2. The ArrayList was part of the Java Collections Framework. The ArrayList class was designed from the ground up to work as part of the Collection Framework and all its common interfaces, whereas the Vector class had to later be retrofitted into the Collections Framework. A primary difference between a Vector and an ArrayList is that Vector is synchronized whereas ArrayList is not. Synchronization has to do with threading. Generally, unless there is a specific reason not to, you should use an ArrayList rather then a Vector (and this holds pretty true for new developers, until they get into more complex issues/code in which there is a reason to use a Vector.) Like all collections classes, Vectors and ArrayLists can only hold objects. They cannot hold primitives directly; primitives need to be wrapped with a wrapper class such as Integer for an int. Java 5.0 does this "automatically" via a feature called autoboxing.

An Array, which in Java is an object, is not part of the Collections framework. While Arrays are can hold objects or primitives, they are most useful with primitives. Because an Array is not part of the Collections API, they are a little bit less flexible. Also, one of the biggest shortcomings of an Array is that it is fixed in size. You cannot expand or contrast an Array. So if you make an Array to hold 10 items, and you suddenly need to put in 11, you need to create a new Array of size 11, copy all the items over, then add your 11th item. This can be a costly operation. ArrayLists (and Vectors) on the other hand can grow and shrink as needed. Therefore ArrayLists are far more useful to hold dynamic collections, or collections where the size is hard to predetermine.

Take a look at the Sun Java Tutorial and theThinking in Java links above. They should help you to understand in more detail the differences of the items. You can also try a Google search using the terms ArrayList Vector difference Java and ArrayList Array difference Java to find some other articles and information. Also search the "Java in General (Beginner)" forum here using the search ArrayList Vector difference and you will find a dozen or so threads discussing the differences.
[ July 17, 2005: Message edited by: Mark Vedder ]
John K. Smith
Greenhorn

Joined: Jul 17, 2005
Posts: 5
An array is fixed at compile time.

An ArrayList is a non-thread safe array whose size can dynamically change during run-time. Its really an array which is shifted into another array at least one size bigger than the original during run-time.

A vector is similar to an ArrayList, but with thread-safe capability. So any thread working on a vector will do so in knowledge that no other thread will be working on any object referenced from within the vector.
Jacquie Barker
author
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
Do you understand the basic difference between an interface and a class? "Collection" (capital C) is an interface that various classes implement (e.g., Vector, ArrayList, TreeSet, etc.) -- hence, all of these classes can be used interchangeably anywhere in a program that a Collection is required.

The term "collection" when used in general refers to an object designed to hold onto references to other objects, just like an egg carton is a special type of object designed to hold other objects (eggs). There are other classes in Java that *don't* implement the Collection interface, but which nonetheless serve as a collection in the lower-case c sense of the word: e.g., Array, TreeMap, etc.

Regards,

Jacquie


Author of Beginning Java Objects, Beginning C# Objects, and Taming the Technology Tidal Wave
Jacquie Barker
author
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
P.S. Please see my post about various types of collection in the Java in General (intermediate) forum under the topic "Navigating and referencing in a ListIterator".
yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
Hi all, Thank you very much for clearing this doubt which I had for a long time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList Vector Collections Array
 
Similar Threads
How to pass Java vector to a web service using Apache AXIS
How to order my DB results using servlets??
convert collections to string
arraylists
advantage Doublylink list over linklist