1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
public class Chapitre2CoreJava2{
public static List<String> listeDesMots()
{
List<String> liste;
liste = new ArrayList<String>();
String s = "toto pospos ; totot, totot, tata tata ";
StringTokenizer st = new StringTokenizer(s,"[](){};, :.\n\"");
System.out.println("Nombre de mots" + st.countTokens());
while (st.hasMoreTokens())
{/** Tant qu'il existe encore des éléments */
System.out.println(st.nextToken());
}
return liste;
}
public static Map<String,Integer> OccurrencesDesMots(){
List <String> liste = listeDesMots();
int cpt = 0; /**création d'un compteur*/
Map<String,Integer> table = new HashMap<String,Integer>(); /**associe une clef à une valeur et de retrouver une clef à partir d'une valeur*/
Iterator it = liste.iterator(); /** créateur d'un itérateur pour parcourir la liste */
while(it.hasNext())/** tant que le liste tjs un element on se place a l'element suivant*/
{
for(int i=0;i<liste.size();i++)
{
String s = (String) it.next();
System.out.println(s);
Iterator it2 = liste.iterator();
while(it2.hasNext())
{
for(int j=0;j<liste.size();j++)
{
String s2 = (String) it2.next();
if (s.equals(s2))
{
cpt += 1;
System.out.println("compteur"+cpt);
}
}
}
table.put(s,cpt);
System.out.println("clé:"+ table.keySet() +"valeur"+ table.values());
}
}
return table;
} |