package com.codemonkey;
import static java.lang.System.out;
import static java.lang.System.err;
import java.util.Arrays;
import java.util.function.Predicate;//Predicado: reciben un argumento y devuelven un valor logico
//Predicate<T>
// Predicate<T> and(Predicate<? super T> other)
// Predicate<T> isEqual(Object targetRef)
// Predicate<T> negate()
// Predicate<T> or(Predicate<? super T> other)
// boolean test(T t)
import java.util.function.UnaryOperator;
import java.util.List;
import java.util.ArrayList;
//ArrayList: estructura de datos basada en posición(indices)
public class Main{
public static void main(String[] args){
ArrayListDemo();
}
public static void ArrayListDemo(){
ArrayList listaStr = new ArrayList();
listaStr.add("gato");//0,gato
listaStr.add("perro");//1,perro
listaStr.add(2,"canario");//2,canario
listaStr.add(3,new String("pez"));//3,pez
//Tam = 4, indices [0,1,2,3]
int tam = listaStr.size();
out.printf("Tam:%d\n",tam);//4
out.printf("%s\n",listaStr.toString());//[gato, perro, canario, pez]
boolean agregarTodo = listaStr.addAll(listaMascotas());
String agregado = agregarTodo? "Agregado" : "No agregado";
out.printf("%s\n",agregado);//Agregado
tam = listaStr.size();
out.printf("Tam:%d\n",tam);//6
out.printf("%s\n",listaStr.toString());//[gato, perro, canario, pez, tortuga, lagartija]
//Limpiamos listaMascotas
listaMascotas().clear();
//Sigue teniendo elementos
out.println("Clear:"
+listaMascotas().toString());//[tortuga, lagartija]
out.printf("Lista:%s\n",listaStr.toString());
listaStr.clear();
// listaStr esta vacia
out.printf("Lista:%s\n",listaStr.toString());
if(listaStr.isEmpty()){
for(String e: listaMascotas()){
listaStr.add(e);
}
}
out.printf("Lista:%s\n",listaStr.toString());//[tortuga, lagartija]
out.printf("Tam:%d\n",listaStr.size());//2
String contieneTortuga = listaStr.contains("tortuga")? "Contiene tortuga" : "No contiene tortuga";
out.printf("%s\n",contieneTortuga);//Contiene tortuga
listaStr.ensureCapacity(23);
out.printf("Tam:%d\n",listaStr.size());//2
listaStr.stream().forEach(out::println);//tortuga lagartija
// add es para agregar
listaStr.add("mariposa");
listaStr.add("mariposa");
listaStr.add("mariposa");
out.println(listaStr.toString());//[tortuga, lagartija, mariposa, mariposa, mariposa]
//[tortuga, lagartija, mariposa, mariposa]
// set es para actualizar
listaStr.set(3, "canario");
listaStr.set(4, "alacran");
out.println(listaStr.toString());//[tortuga, lagartija, mariposa, canario, alacran]
out.printf("Tam:%d\n",listaStr.size());//5
//[tortuga, lagartija, mariposa, canario, alacran]
//[0, 1 , 2, 3, 4]
int indice = 3;
//Es necesario hcer un cast
String getCanario = (String) listaStr.get(indice);
out.printf("Get:%s\n",getCanario);//canario
//[*tortuga, lagartija, mariposa, canario, alacran]
//[*0, 1 , 2, 3, 4]
int indiceDe = listaStr.indexOf("tortuga");
out.println(indiceDe);//0
//[tortuga, lagartija, mariposa, canario, alacran]
//[0, 1 , *2, 3, 4]
indiceDe = listaStr.indexOf(0);
out.println(indiceDe);//-1
//[tortuga, *lagartija, mariposa, canario, alacran]
//[0, *1 , 2, 3, 4]
indiceDe = listaStr.indexOf("lagartija");
out.println(indiceDe);//1
java.util.Iterator<String> itr = listaStr.iterator();
while (itr.hasNext()) {
String element = itr.next();
out.print(element + ",\n");
}//tortuga, lagartija, mariposa, canario, alacran,
java.util.ListIterator<String> litr = listaStr.listIterator();
while (litr.hasNext()) {
String element = litr.next();
litr.set(element + " ");
}
while (litr.hasPrevious()) {
String element = litr.previous();
out.println(element + " ");
}
out.printf("%s\n",listaStr.toString());
//[tortuga , lagartija , mariposa , canario , *alacran ]
//[0, 1 , 2, 3, *4]
int ultimoIndiceDe = listaStr.lastIndexOf("alacran");
out.println(ultimoIndiceDe);//-1
//Remover elementos
//[*tortuga, lagartija, mariposa, canario, alacran ]
//[*0, 1 , 2, 3, 4]
listaStr.remove(0);
out.printf("%s\n",listaStr.toString());//[lagartija, mariposa, canario, alacran ]
//[*lagartija, mariposa, canario, alacran ]
//[*0, 1 , 2, 3]
listaStr.remove(0);
out.printf("%s\n",listaStr.toString());//[mariposa, canario, alacran ]
//[*mariposa, canario, alacran ]
//[*0, 1 , 2]
listaStr.remove(0);
out.printf("%s\n",listaStr.toString());//[canario, alacran ]
tam = listaStr.size();
out.printf("Tam:%d\n",tam);//2
boolean removerTodo = listaStr.removeAll(listaStr);
String vacio= removerTodo? "Esta vacia" : "Aun quedan elementos";
tam = listaStr.size();
out.printf("Tam:%d\n",tam);//0
out.printf("%s\n",vacio);//Esta vacia
listaStr.add(0,"unicornio");
listaStr.add(1,"pegaso");
tam = listaStr.size();
out.printf("%s\n",listaStr.toString());//[unicornio, pegaso]
out.printf("Tam:%d\n",tam);//2
listaStr.add(2,"documento.xml");
listaStr.add(3,"documento.pdf");
listaStr.add(4,"archivo.xml");
tam = listaStr.size();
out.printf("%s\n",listaStr.toString());//[unicornio, pegaso, documento.xml, documento.pdf, archivo.xml]
out.printf("Tam:%d\n",tam);//5
Predicate<String> predicateList = l -> l.contains("documento.xml") || l.contains("documento.pdf") || l.contains("archivo.xml") ;
listaStr.removeIf(predicateList);
tam = listaStr.size();
out.printf("%s\n",listaStr.toString());//[unicornio, pegaso]
out.printf("Tam:%d\n",tam);//2
listaStr.add(2,"ballena");
listaStr.add(3,"halcon");
listaStr.add(4,"rana");
listaStr.add(5,"armadillo");
listaStr.add(6,"hormiga");
tam = listaStr.size();
out.printf("%s\n",listaStr.toString());//[unicornio, pegaso, ballena, halcon, rana, armadillo, hormiga]
out.printf("Tam:%d\n",tam);//7
//Remover rango
ArrayListDemo arrlist = new ArrayListDemo();
out.println("Lista:" + arrlist);//[10,12,31]
//arrlist.removeRange(0,2);
//out.println("Resultado:" + arrlist);
MyOperator<String> operator = new MyOperator<> ();
operator.varc1 = "gato";
out.printf("%s\n",listaStr.toString());//[[unicornio, pegaso, ballena, halcon, rana, armadillo, hormiga]
listaStr.replaceAll(operator);
out.printf("%s\n",listaStr.toString());//[gato, gato, gato, gato, gato, gato, gato]
listaStr.clear();
tam = listaStr.size();
out.printf("%s\n",listaStr.toString());//[]
out.printf("Tam:%d\n",tam);//0
listaStr.add(0,"iguana");
listaStr.set(0,"camelia");
listaStr.add(1,"rana");
listaStr.set(1,"rosa");
tam = listaStr.size();
out.printf("%s\n",listaStr.toString());//[camelia,rosa]
out.printf("Tam:%d\n",tam);//2
listaStr.add("tulipan");
listaStr.add("clavel");
listaStr.add("margarita");
tam = listaStr.size();
out.printf("%s\n",listaStr.toString());//[camelia, rosa, tulipan, clavel, margarita]
out.printf("Tam:%d\n",tam);//5
ArrayList lista = new ArrayList<>();
lista.add("clavel");
lista.add("margarita");
lista.add("tulipan");
listaStr.retainAll(lista);
for(Object el : listaStr){
out.println(el);
}//tulipan clavel margarita
tam = listaStr.size();
out.printf("%s\n",listaStr.toString());//[tulipan, clavel, margarita]
out.printf("Tam:%d\n",tam);//3
java.util.Collections.sort(listaStr);
out.printf("%s\n",listaStr.toString());//[clavel, margarita, tulipan]
Object[] arreglo = listaStr.toArray();
for(int i=0; i < arreglo.length; i++){
out.println(arreglo[i]);
}
listaStr.add("rosa");
listaStr.add("camelia");
//[clavel, margarita, tulipan,rosa,camelia]
//[0,1,*2, 3, *4]
//Tam:5
List<Object> listaObj = listaStr.subList(2, 4);
out.println(listaObj.toString()); //[tulipan,rosa]
out.println(listaStr.toString());//[clavel, margarita, tulipan,rosa,camelia]
}
static ArrayList<String> listaMascotas(){
ArrayList<String> mascotas = new ArrayList<String>(Arrays.asList(new String("tortuga"), "lagartija"));
return mascotas;
}
}
class ArrayListDemo extends ArrayList{
ArrayListDemo(){
add(10);
add(12);
add(31);
}
}
class MyOperator<T> implements UnaryOperator<T>{
T varc1;
public T apply(T varc){
return varc1;
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.