Hashtable sortere efter ?
Hvad sortere en Hashtable efter når man ligger key/værdi deri..??Ud fra disse 2 klasser kan jeg ikk se logikken...
------------------------------------------
import java.util.*;
public class Spillere
{
private Hashtable spillere;
public Spillere()
{
spillere = new Hashtable();
}
public void tilfoejSpiller(String navn)
{
if(!spillere.containsKey(navn))
spillere.put(navn,new Integer(0));
}
public void fjernSpiller(String navn)
{
if(spillere.containsKey(navn))
spillere.remove(navn);
}
public void opdaterPoint(String navn, int point)
{
if (spillere.containsKey(navn))
{
int nyPoint = Integer.parseInt(spillere.get(navn).toString()) + point;
spillere.put(navn,new Integer(nyPoint));
}
}
public Hashtable hentSpillere()
{
return spillere;
}
public String visSpilstyre()
{
Enumeration e = spillere.keys();
Object a = e.nextElement();
return a.toString();
}
public int antalSpillere()
{
int antal = 0;
Enumeration e = spillere.keys();
while( e.hasMoreElements() )
{
Object a = e.nextElement();
antal++;
}
return antal;
}
}
----------------------------------------------
import java.util.*;
public class Main
{
private static Spillere sp;
private static Hashtable tmpSp;
private static String spillerListe;
public static void main(String args[])
{
sp = new Spillere();
sp.tilfoejSpiller("Anne");
sp.tilfoejSpiller("Anders");
sp.tilfoejSpiller("Jens");
sp.tilfoejSpiller("Bjørn");
System.out.println(sp.hentSpillere());
System.out.println(sp.visSpilstyre());
sp.fjernSpiller("Anne");
System.out.println(sp.hentSpillere());
System.out.println(sp.visSpilstyre());
sp.tilfoejSpiller("Anne");
System.out.println(sp.hentSpillere());
System.out.println(sp.visSpilstyre());
sp.fjernSpiller("Anne");
System.out.println(sp.hentSpillere());
System.out.println(sp.visSpilstyre());
sp.tilfoejSpiller("Annn");
System.out.println(sp.hentSpillere());
System.out.println(sp.visSpilstyre());
System.out.println(sp.hentSpillere());
System.out.println(sp.visSpilstyre());
sp.tilfoejSpiller("Anne");
System.out.println(sp.hentSpillere());
System.out.println(sp.visSpilstyre());
sp.tilfoejSpiller("Anna");
System.out.println(sp.hentSpillere());
System.out.println(sp.visSpilstyre());
sp.tilfoejSpiller("Aage");
System.out.println(sp.hentSpillere());
System.out.println(sp.visSpilstyre());
}
}
-Anders
