• [^] # Re: Typage

    Posté par . En réponse au message débutant java : opérations de base sur les listes. Évalué à 1.

    Salut,

    Regarde.

    Mon code actuel :

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.List;
    public class ClassA {
     // here, you just say what types are accepted
     private class ClassB<I extends Integer, S extends String> {
     // it's a bit more wrapping, but for the fun
     private List<Object> l;
     public ClassB() {
     l = new ArrayList<Object>();
     }
     private void add(Class c, Object... params) throws Exception {
     l.addAll(checkIn(c, params));
     }
     // here we can do a bunch of checks. Choose your own. It's just the entry gate
     private Collection<? extends 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 Object get(int i) {
     return l.get(i);
     }
     public int size() {
     return l.size();
     }
     private int getNumerOfNumbers() {
     int r=0;
     for(Object o:l) {
     if(o instanceof Number)
     r++;
     }
     return r;
     }
     // here we just want to do something more subtle than print
     public double getMean() throws Exception {
     int sum = this.getNumerOfNumbers();
     if(sum==0)
     throw new Exception("Maybe you want numbers...");
     double mean=0;
     for(Object o:l) {
     if(o instanceof Number) {
     mean+=((Number)o).doubleValue()/sum;
     }
     }
     return mean;
     }
     }
     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<>();
     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
     for(int i=0; i< maListeDeBidules.size(); i++)
     System.out.println("mon bidule "+(i+1)+" : "+maListeDeBidules.get(i)); 
     System.out.println("La moyenne dans ma liste de bidules est de : "+maListeDeBidules.getMean());
     // here we go!
     ClassB<Integer, String> maListeDeBidules2 = new ClassB<>();
     for(int i=0; i<10000000; i++)
     maListeDeBidules2.addInt(i); 
     }
    }

    Et le tien que j'ai modifié un tout petit peu (le moins possible) pour faire un petit tour sur une grosse liste d'entiers.

    import java.util.ArrayList;
    import java.util.List;
    class ListeExtensible {
     interface MyStuff { 
     public void print();
     }
     class MyInt implements MyStuff {
     final int i;
     public MyInt(int i) {
     this.i = i;
     }
     public void print() {
     System.out.println(this.i);
     }
     }
     class MyString implements MyStuff {
     final String s;
     public MyString(String s) {
     this.s = s;
     }
     public void print() {
     System.out.println(this.s);
     }
     }
     public void doStuff() {
     List<MyStuff> maListe = new ArrayList<>();
     maListe.add(new MyInt(1));
     maListe.add(new MyString("hello"));
     for (MyStuff element : maListe) {
     element.print();
     }
     List<MyStuff> maListe2 = new ArrayList<>();
     for(int i=0; i<10000000; i++)
     maListe2.add(new MyInt(i)); 
     }
     public static void main(String[] args) {
     ListeExtensible l = new ListeExtensible();
     l.doStuff();
     }
    }

    Alors maintenant on peut regarde ce qui se passe en mémoire :
    Mon code :

    Code_kaos

    Ton code :

    Code_autre

    Je sais pas si tu vois la petite subtile différence d'approche.

    Matricule 23415