Je suppose que tu vas toujours pas être content de ma solution, mais c'est pas grave.
importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Collection;importjava.util.Iterator;importjava.util.LinkedList;importjava.util.List;publicclassClassA{// here, you just say what types are acceptedprivateclassClassB<IextendsInteger,SextendsString>implementsIterable{// it's a bit more wrapping, but for the funprivateCollectionc;publicClassB(Collectionc){// we could do better, just a bit of safety hereif(!c.isEmpty())c.clear();this.c=c;}privatevoidadd(Classc,Object...params)throwsException{this.c.addAll(checkIn(c,params));}// here we can do a bunch of checks. Choose your own. It's just the entry gateprivateCollection<Object>checkIn(Classc,Object...params)throwsException{if(params.length==1&¶ms[0].getClass().equals(c))returnArrays.asList(params[0]);//a bit ugly here. Something better can be doneif(params[0].getClass().equals(c)){returnArrays.asList(params);}thrownewException("You shall not pass!");}publicvoidaddInt(Ii)throwsException{add(i.getClass(),i);}publicvoidaddManyInts(I...i)throwsException{add(i[0].getClass(),i);}publicvoidaddString(Ss)throwsException{add(s.getClass(),s);}publicvoidaddManyStrings(S...s)throwsException{add(s[0].getClass(),s);}publicintsize(){returnc.size();}privateintgetNumerOfBidules(Classc){intr=0;for(Objecto:this.c){if(c.isInstance(o))r++;}returnr;}publicCollectionfilter(Classc){Listr=newArrayList();for(Objecto:this.c){if(c.isInstance(o))r.add(o);}returnr;}publicIteratoriterator(){returnc.iterator();}}publicstaticvoidmain(String[]args)throwsException{ClassAa=newClassA();a.doThings();}publicvoiddoThings()throwsException{ClassB<Integer,String>maListeDeBidules=newClassB<>(newLinkedList());maListeDeBidules.addInt(1);// okmaListeDeBidules.addString("deux");// okmaListeDeBidules.addManyInts(3,4,5,6,7,8);// okmaListeDeBidules.addManyStrings("neuf","dix","onze","douze","treize","quatorze");// okinti=0;for(Objecto: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=newClassB<>(newArrayList());for(i=0;i<10000000;i++)maListeDeBidules2.addInt(i);}publicdoublegetMean(ClassBb)throwsException{Classc=Number.class;intn=b.getNumerOfBidules(c);if(n==0)thrownewException("Maybe you want numbers...");doublemean=0;for(Objectv:b.filter(c)){mean+=((Number)v).doubleValue()/n;}returnmean;}}
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).
[^] # Re: Typage
Posté par _kaos_ . 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.
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