mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-10 02:13:07 +01:00
feat(ch 5 & 6): examples added
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package tutorialJava.capitulo6_ColeccionesDeDatos.ejemplo01_ListYArrayList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class Ejemplo01_ListasConInteger {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Declarado la lista de Integers
|
||||
List<Integer> numeros = new ArrayList<Integer>();
|
||||
|
||||
// Inicializar con 10 elementos de tipo Integer
|
||||
for (int i = 0; i < 10; i++) {
|
||||
numeros.add(new Integer(100 + i));
|
||||
numeros.add(Integer.valueOf(100 + i));
|
||||
numeros.add(100 + i);
|
||||
}
|
||||
|
||||
// Agrego un elemento
|
||||
numeros.add(1000);
|
||||
|
||||
// Elimino un elemento
|
||||
numeros.remove(2);
|
||||
|
||||
// Inserto un elemento en una posición concreta
|
||||
numeros.add(1, 5000);
|
||||
|
||||
// Crear una nueva lista
|
||||
List<Integer> numerosNegativos = new ArrayList<Integer>();
|
||||
|
||||
// Inicializo la nueva lista
|
||||
for (int i = 0; i < 3; i++) {
|
||||
numerosNegativos.add(-1000 + i);
|
||||
}
|
||||
|
||||
// Agrego la nueva lista en cualquier posición de la antigua lista
|
||||
numeros.addAll(4, numerosNegativos);
|
||||
|
||||
// Primera forma de eliminar un elemento concreto sin conocer su posición
|
||||
// Tengo que recorrer todos los elementos de la lista
|
||||
for (int i = 0; i < numeros.size(); i++) {
|
||||
if (numeros.get(i).intValue() == 5000) {
|
||||
numeros.remove(i);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package tutorialJava.capitulo6_ColeccionesDeDatos.ejemplo01_ListYArrayList.cromosBaloncesto;
|
||||
|
||||
public class CromoBaloncesto {
|
||||
private int id;
|
||||
private String nombre;
|
||||
/**
|
||||
* @param id
|
||||
* @param nombre
|
||||
*/
|
||||
public CromoBaloncesto(int id, String nombre) {
|
||||
this.id = id;
|
||||
this.nombre = nombre;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CromoBaloncesto [id=" + id + ", nombre=" + nombre + "]";
|
||||
}
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package tutorialJava.capitulo6_ColeccionesDeDatos.ejemplo01_ListYArrayList.cromosBaloncesto;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class EjemploListasCromos {
|
||||
|
||||
public static void main (String args[]) {
|
||||
CromoBaloncesto c1 = new CromoBaloncesto(100, "Felipe Reyes");
|
||||
CromoBaloncesto c2 = new CromoBaloncesto(200, "Lebron");
|
||||
CromoBaloncesto c3 = new CromoBaloncesto(300, "Pau Gasol");
|
||||
|
||||
List<CromoBaloncesto> lista = new ArrayList<CromoBaloncesto>();
|
||||
lista.add(c1);
|
||||
lista.add(c2);
|
||||
lista.add(10, c3);
|
||||
|
||||
for (CromoBaloncesto c : lista) {
|
||||
System.out.println(c.getNombre());
|
||||
}
|
||||
|
||||
|
||||
|
||||
// CromoBaloncesto c4 = new CromoBaloncesto(4, "Cristina Castro");
|
||||
// CromoBaloncesto c5 = new CromoBaloncesto(5, "María Jesús Joyanes");
|
||||
// CromoBaloncesto c6 = new CromoBaloncesto(6, "Alazne Ruiz");
|
||||
//
|
||||
// List<CromoBaloncesto> lista2 = new ArrayList<CromoBaloncesto>();
|
||||
// lista2.add(c4);
|
||||
// lista2.add(c5);
|
||||
// lista2.add(c6);
|
||||
//
|
||||
// lista.addAll(1, lista2);
|
||||
//
|
||||
// lista.removeAll(lista);
|
||||
//
|
||||
// System.out.println("VAcía: " + lista.isEmpty());
|
||||
//
|
||||
// for (CromoBaloncesto c : lista) {
|
||||
// System.out.println(c.toString());
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package tutorialJava.capitulo6_ColeccionesDeDatos.ejemplo02_HashMap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class EjemploHashMap {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static void ejemploHashMapGeneral () {
|
||||
// Creaci<63>n de un HashMap general
|
||||
HashMap hm = new HashMap();
|
||||
|
||||
// Inserci<63>n de datos en un HashMap general
|
||||
hm.put("1", new Persona ("11111111A", "P<EFBFBD>rez P<>rez", "Jos<EFBFBD>"));
|
||||
hm.put("2", new Persona ("22222222A", "Mu<EFBFBD>oz Mu<4D>oz", "Rafael"));
|
||||
hm.put("3", new Persona ("33333333A", "Cuenca Cuenca", "Juan"));
|
||||
hm.put("4", new Persona ("44444444A", "Torralbo Torralbo", "Patricia"));
|
||||
hm.put("5", new Persona ("55555555A", "L<EFBFBD>rida L<>rida", "Luc<EFBFBD>a"));
|
||||
|
||||
|
||||
// Recuperaci<63>n de datos en un HashMap general
|
||||
System.out.println("Objeto con id 1: " + hm.get("1")); // Recuperaci<63>n de un solo objeto
|
||||
|
||||
// Inserci<63>n de un objeto con un tipo de identificador diferente
|
||||
hm.put(Integer.valueOf(6), new Persona ("66666666A", "Ram<EFBFBD>rez Ram<61>rez", "Ramiro"));
|
||||
|
||||
|
||||
// Recuperaci<63>n de todas las claves de objetos en el HM
|
||||
System.out.println("\nRecorrido del HashMap:");
|
||||
Object claves[] = hm.keySet().toArray();
|
||||
for (int i = 0; i < claves.length; i++) {
|
||||
System.out.println("Identificador: " + claves[i] +
|
||||
" - Objeto obtenido: " + hm.get(claves[i]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static void ejemploHashMapEspecifico () {
|
||||
// Creaci<63>n de un HashMap general
|
||||
HashMap<String, Persona> hm = new HashMap<String, Persona>();
|
||||
|
||||
// Inserci<63>n de datos en un HashMap general
|
||||
hm.put("1", new Persona ("11111111A", "P<EFBFBD>rez P<>rez", "Jos<EFBFBD>"));
|
||||
hm.put("2", new Persona ("22222222A", "Mu<EFBFBD>oz Mu<4D>oz", "Rafael"));
|
||||
hm.put("3", new Persona ("33333333A", "Cuenca Cuenca", "Juan"));
|
||||
hm.put("4", new Persona ("44444444A", "Torralbo Torralbo", "Patricia"));
|
||||
hm.put("5", new Persona ("55555555A", "L<EFBFBD>rida L<>rida", "Luc<EFBFBD>a"));
|
||||
|
||||
// Eliminaci<63>n de alg<6C>n elemento en el HM
|
||||
hm.remove("5");
|
||||
|
||||
// Recuperaci<63>n de todos los objetos en el HM
|
||||
System.out.println("\nRecorrido de los objetos del HashMap:");
|
||||
Iterator<Persona> personas = hm.values().iterator();
|
||||
while (personas.hasNext()) {
|
||||
System.out.println("Objeto obtenido: " + personas.next());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main (String args[]) {
|
||||
ejemploHashMapGeneral();
|
||||
//ejemploHashMapEspecifico();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package tutorialJava.capitulo6_ColeccionesDeDatos.ejemplo02_HashMap;
|
||||
|
||||
public class Persona {
|
||||
|
||||
private String Dni;
|
||||
private String Apellidos;
|
||||
private String Nombre;
|
||||
|
||||
|
||||
public Persona(String dni, String apellidos, String nombre) {
|
||||
super();
|
||||
Dni = dni;
|
||||
Apellidos = apellidos;
|
||||
Nombre = nombre;
|
||||
}
|
||||
|
||||
|
||||
public String getDni() {
|
||||
return Dni;
|
||||
}
|
||||
|
||||
|
||||
public void setDni(String dni) {
|
||||
Dni = dni;
|
||||
}
|
||||
|
||||
|
||||
public String getApellidos() {
|
||||
return Apellidos;
|
||||
}
|
||||
|
||||
|
||||
public void setApellidos(String apellidos) {
|
||||
Apellidos = apellidos;
|
||||
}
|
||||
|
||||
|
||||
public String getNombre() {
|
||||
return Nombre;
|
||||
}
|
||||
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
Nombre = nombre;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Persona [Dni=" + Dni + ", Apellidos=" + Apellidos + ", Nombre=" + Nombre + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package tutorialJava.capitulo6_ColeccionesDeDatos.ejemplo02_HashTable;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
|
||||
public class EjemploHashTable {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Hashtable ht = new Hashtable();
|
||||
ht.put("st01", "String 01");
|
||||
ht.put("st02", "String 02");
|
||||
ht.put("st03", "String 03");
|
||||
ht.put("st04", "String 04");
|
||||
|
||||
ht.remove("st04");
|
||||
|
||||
ht.size();
|
||||
|
||||
Enumeration en = ht.elements();
|
||||
while (en.hasMoreElements()) {
|
||||
Object obj = en.nextElement();
|
||||
System.out.println("Elemento: " + obj.toString());
|
||||
}
|
||||
|
||||
|
||||
en = ht.keys();
|
||||
while (en.hasMoreElements()) {
|
||||
System.out.println("Elemento id: " + en.nextElement() +
|
||||
" - Obj: " + ht.get(en.nextElement()));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user