Hoy el tema que nos ocupa es uno sencillo, pero que no debe pasarse por alto, por ser estas clases unas de las más usadas, dado su sencillo funcionamiento.
En primer lugar, vamos a inicializar un array de String, que como sabéis tiene longitud fija, lo que es un pequeño inconveniente a la hora de trabajar con algunos conjuntos de elementos. Para solucionar este problema, tenemos otras clases llamadas colecciones.
Dos de ellas son ArrayList y Vector. Un ArrayList es una implementación de la clase List, que es una colección que admite elementos duplicados y nulos, así como acceso por índice y otros métodos para trabajar con sus elementos. Vector también hereda de List es muy similar a ArrayList, con la diferencia de que la clase Vector es segura para trabajar con hilos (sincronizada). Esta ventaja penaliza ligeramente el rendimiento del Vector.
Antes de mostrar un ejemplo, añadir que a partir de la versión 1.6 1.5 de Java, si no me equivoco (me equivoqué :P), se introdujeron las colecciones genéricas, que suponen una ventaja en cuanto a que soportan comprobación de tipos en tiempo de compilación. Para definir el tipo de la colección simplemente se añade el tipo del objeto entre los símbolos '<' y '>' (ej.: Vector<String>).
A continuación, un sencillo ejemplo de uso de estas clases.
Hola!!
Muchas gracias por la info!! sólo decirte que las colecciones genéricas se introdujeron en la versión 1.5 de java.
Un saludo,
Nuria
Gracias Nuria!! ahora lo corrijo. Lo puse para ver si alguien se daba cuenta, jeje ;)
Un saludo!
tengo un problemilla con el que creo que me podeis ayudar. tengo un random y quiero asignarlo a un array, pero no lo consigo. Alguna idea?
package feedback;
import java.util.HashSet;
/**
*
* @author Pere
*/
public class primitiva {
public static void main(String args[]) {
HashSet t=new HashSet();
while (t.size()<9) {
Integer al=new Integer(10 + new java.util.Random().nextInt(91));
t.add(al);
}
java.util.List lista=new java.util.ArrayList(t);
java.util.Collections.sort(lista);
// Se muestran los resultados
java.util.Iterator it=lista.iterator();
while (it.hasNext()) {
System.out.println((Integer)it.next());
}
String []strArray = new String[9];
ListArray.toArray(strArray);
}
}
World's Best Neck Massager Get it Now 50% OFF + Free Shipping!
Wellness Enthusiasts! There has never been a better time to take care of your neck pain!
Our clinical-grade TENS technology will ensure you have neck relief in as little as 20 minutes.
Get Yours: https://hineck.co
The Best,
Shirleen
Utilización básica de arrays, ArrayList y Vector. Conversión de arrays. | Java-Spain.com
Hello
Looking to improve your posture and live a healthier life? Our Medico Postura™ Body Posture Corrector is here to help!
Experience instant posture improvement with Medico Postura™. This easy-to-use device can be worn anywhere, anytime – at home, work, or even while you sleep.
Made from lightweight, breathable fabric, it ensures comfort all day long.
Grab it today at a fantastic 60% OFF: https://medicopostura.com
Plus, enjoy FREE shipping for today only!
Don't miss out on this amazing deal. Get yours now and start transforming your posture!
Kind Regards,
Junko
Good day,
I hope you're doing well. I wanted to let you know about our new BANGE backpacks and sling bags that just released.
The bags are waterproof and anti-theft, and have a built-in USB cable that can recharge your phone while you're on the go.
Both bags are made of durable and high-quality materials, and are perfect for everyday use or travel.
Order yours now at 50% OFF with FREE Shipping: http://bangeshop.com
Cheers,
Rosalie
Hey there,
If you are one of the sufferers of the common problems nails have, then you are in luck! Our Toenail Clippers is here to help. It has a specially designed clip that can help those with troubles with winding nails, hard nails, two nails, nail cracks, deep nails, and thickened nails.
We are confident that our Toenail Clippers will provide you with the results you are looking for.
Get yours today with 60% OFF: https://podiatristusa.sale
Best,
Katharina
Get The Worlds Greatest Magic Sand Free Beach Mat!
Watch sand, dirt & dust disappear right before your eyes! It's perfect for beach, picnic, camping or hiking.
Act Now And Receive A Special Discount For Our Magic Mat!
Get Yours Here: https://magicmats.co
Have a great time,
Elsie
Good day
I wanted to reach out and let you know about our new dog harness. It's really easy to put on and take off - in just 2 seconds - and it's personalized for each dog.
Plus, we offer a lifetime warranty so you can be sure your pet is always safe and stylish.
We've had a lot of success with it so far and I think your dog would love it.
Get yours today with 50% OFF: https://caredogbest.com
FREE Shipping - TODAY ONLY!
To your success,
Hung
Enviar un comentario nuevo