Today we have chosen a simple-but-no-least topic due to the common use of all of these classes and the utilities they provide us when manipulating sets of items.
First of all, we are going to initialize an array of String, whose size is determined at the moment of creation. To avoid this 'problem' we have some other collections.
Two of them are ArrayList and Vector. An ArrayList is an implementation of List, which is a collection that admits duplicated and null elements, as well as indexed access and some other methods to handle its elements. Vector class also inherits from List and behaves almost like ArrayList with some differences: Vector is 'thread safe' which means that if we intend to modify a Vector from two different threads, it will throw an exception. It is also called a syncronized class. This advantage implies a small handicap when talking about performance.
Before giving an example, I would like to recall that since version 1.5 of Java, if I am right, generic collections were introduced, and that involves an improvement as they support compile-time type checking. To define the type of the collection used, simply add 'less than' and 'greater than' simbols: '<' & '>' (e.g. Vector<String>).
Now, it is time for an example:
Basic usage of arrays, ArrayList and Vector. Converting arrays.
By Álvaro Lázaro - Escrito el Marzo 2nd, 2010
Enviar un comentario nuevo