10.05.2014 Views

programación i - Universidad ORT Uruguay

programación i - Universidad ORT Uruguay

programación i - Universidad ORT Uruguay

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

162 Programación I<br />

miLista = new ArrayList();<br />

Camion unCamion = new Camion( "blanco",123 );<br />

//Agrego el camion a la lista<br />

miLista.add( unCamion );<br />

//Creo otros tres camiones<br />

Camion camionDos = new Camion( "azul", 789 );<br />

Camion camionTres = new Camion( "verde", 456 );<br />

Camion camionCuatro = new Camion( "rojo", 567 );<br />

//Los agrego a la lista<br />

miLista.add( camionDos );<br />

miLista.add( camionTres );<br />

miLista.add( camionCuatro );<br />

//Indico la posición del elemento unCamion<br />

System.out.println( "La posición de unCamion es: "+miLista.indexOf( unCamion ) );<br />

//Indico si la lista tiene un objeto con chapa 123<br />

Camion aux = new Camion( "", 123 );<br />

if( miLista.contains( aux ) ) {<br />

System.out.println( "miLista tiene un objeto con chapa 123" );<br />

}<br />

else {<br />

System.out.println( "miLista NO tiene un objeto con chapa 123" );<br />

}<br />

//Muestro los elementos antes de ordenar usando for<br />

System.out.println( "--- Lista sin ordenar ---" );<br />

for( int i=0; i < miLista.size(); i++ ) {<br />

System.out.println( miLista.get(i) );<br />

}<br />

//Ordeno miLista aprovechando el método compareTo que agregué en Camion que<br />

compara chapas.<br />

Collections.sort( miLista );<br />

//Muestro los elementos ordenados usando un iterator<br />

Iterator miIterator = miLista.iterator();<br />

System.out.println( "--- Lista ordenada ---" );<br />

while( miIterator.hasNext() ){<br />

System.out.println( miIterator.next() );<br />

}<br />

}<br />

}<br />

En la clase Camion se agregó:<br />

import java.util.*;<br />

public class Camion implements Comparable{<br />

.... codigo original

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!