Basic usage of arrays, ArrayList and Vector. Converting arrays.

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:

 

Su voto: Nada Promedio: 1.1 (46 votos)

Enviar un comentario nuevo

El contenido de este campo se mantiene privado y no se mostrará públicamente.
  • Las direcciones de las páginas web y las de correo se convierten en enlaces automáticamente.
  • Etiquetas HTML permitidas: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Saltos automáticos de líneas y de párrafos.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <pre>. The supported tag styles are: <foo>, [foo].

Más información sobre opciones de formato