Created
November 13, 2011 07:12
-
-
Save lfborjas/1361745 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.ArrayList; | |
class Carta{ | |
String palo; | |
int num; | |
public Carta(String palo,int num){ | |
this.palo = palo; | |
this.num = num; | |
} | |
public String toString(){ | |
return String.format("%s%d", palo, num); | |
} | |
} | |
public class grupos{ | |
public static void main (String [] args) | |
{ | |
HashMap<Integer , ArrayList> agrupadas = new HashMap<Integer, ArrayList>(); | |
ArrayList<Carta> cartas = new ArrayList(){{ | |
add(new Carta("G", 5)); | |
add(new Carta("A", 4)); | |
add(new Carta("T", 5)); | |
add(new Carta("A", 5)); | |
add(new Carta("C", 1)); | |
}}; | |
for(Carta carta : cartas){ | |
if (!agrupadas.containsKey(carta.num) ){ | |
//primera vez que se ve este número, hay que crear el grupo | |
agrupadas.put(carta.num, new ArrayList<Carta>()); | |
} | |
//obtener el grupo y agregar esta carta | |
agrupadas.get(carta.num).add(carta); | |
} | |
System.out.println(agrupadas); | |
System.out.println("Aquí tenés cada grupo"); | |
for (ArrayList<Carta> grupo : agrupadas.values()){ | |
System.out.println(grupo); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment