• [^] # Re: Typage

    Posté par . En réponse au message débutant java : opérations de base sur les listes. Évalué à 2. Dernière modification le 15 juillet 2020 à 08:18.

    Salut,

    Je suppose que tu vas toujours pas être content de ma solution, mais c'est pas grave.

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    public class ClassA {
     // here, you just say what types are accepted
     private class ClassB<I extends Integer, S extends String> implements Iterable {
     // it's a bit more wrapping, but for the fun
     private Collection c;
     public ClassB(Collection c) {
     // we could do better, just a bit of safety here
     if(!c.isEmpty())
     c.clear();
     this.c = c;
     }
     private void add(Class c, Object... params) throws Exception {
     this.c.addAll(checkIn(c, params));
     }
     // here we can do a bunch of checks. Choose your own. It's just the entry gate
     private Collection<Object> checkIn(Class c, Object... params) throws Exception {
     if(params.length==1 && params[0].getClass().equals(c))
     return Arrays.asList(params[0]);
     //a bit ugly here. Something better can be done
     if(params[0].getClass().equals(c)) {
     return Arrays.asList(params);
     }
     throw new Exception("You shall not pass!");
     }
     public void addInt(I i) throws Exception {
     add(i.getClass(), i);
     }
     public void addManyInts(I... i) throws Exception {
     add(i[0].getClass(), i);
     }
     public void addString(S s) throws Exception {
     add(s.getClass(), s);
     }
     public void addManyStrings(S... s) throws Exception {
     add(s[0].getClass(), s);
     }
     public int size() {
     return c.size();
     }
     private int getNumerOfBidules(Class c) {
     int r=0;
     for(Object o:this.c) {
     if(c.isInstance(o))
     r++;
     }
     return r;
     }
     public Collection filter(Class c) {
     List r = new ArrayList();
     for(Object o:this.c) {
     if(c.isInstance(o))
     r.add(o);
     }
     return r;
     }
     public Iterator iterator() {
     return c.iterator();
     }
     }
     public static void main(String[] args) throws Exception {
     ClassA a = new ClassA();
     a.doThings();
     }
     public void doThings() throws Exception {
     ClassB<Integer, String> maListeDeBidules = new ClassB<>(new LinkedList());
     maListeDeBidules.addInt(1); // ok
     maListeDeBidules.addString("deux"); // ok
     maListeDeBidules.addManyInts(3,4,5,6,7,8); // ok
     maListeDeBidules.addManyStrings("neuf","dix","onze","douze","treize","quatorze"); // ok
     int i = 0;
     for(Object o:maListeDeBidules) {
     System.out.println("mon bidule "+(i+1)+" : "+o);
     i++;
     }
     System.out.println("La moyenne dans ma liste de bidules est de : "+this.getMean(maListeDeBidules));
     // here we go!
     ClassB<Integer, String> maListeDeBidules2 = new ClassB<>(new ArrayList());
     for(i=0; i<10000000; i++)
     maListeDeBidules2.addInt(i);
     }
     public double getMean(ClassB b) throws Exception {
     Class c = Number.class;
     int n = b.getNumerOfBidules(c);
     if(n==0)
     throw new Exception("Maybe you want numbers...");
     double mean=0;
     for(Object v:b.filter(c)) {
     mean += ((Number)v).doubleValue()/n;
     }
     return mean;
     }
    }

    Voilà, c'est ok pour tout ce qui est collection.

    J'en ai profité pour retirer le calcul de la moyenne de ClassB (rien à faire là dedans).

    Matricule 23415