Pb lors creation d'une classe generic en java pour type primitif Recherche :

Résolu
cs_chien57 Messages postés 17 Date d'inscription vendredi 16 mai 2008 Statut Membre Dernière intervention 22 avril 2009 - 20 mars 2009 à 11:07
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 - 20 mars 2009 à 12:58
lorsque je complile le fichier suivant il me donne le erreur suivante

 
public class TrieurNb<N number>

{

   private boolean croissant;

 
   /**

      cNnstruit un trieur de nombres

    */

   public TrieurNb(boolean croissant)

   {

      this.croissant=croissant;

   }

   

   

    public void tri(N[] tab,int debut, int fin)

    {

        construireTas(tab,debut,fin);

        deconstruireTas(tab,debut,fin);

    }

   

    private void construireTas(N[] tab,int debut, int fin)

    {        

        int j;

         
        for(int i=debut+1;i<fin;i++)

        {

            j=i;

            if(croissant==true)

            {

                while(j>=0 && pere(j)>=0 && tab[pere(j)]<tab[j])

                {

                    echange(tab,pere(j),j);

                    j=pere(j);

                }

            }

            else

            {

                while(j>=0 && pere(j)>=0 && tab[pere(j)]<tab[j])

                {

                    echange(tab,pere(j),j);

                    j=pere(j);

                }

            }

        }

         
    }

 
    private void deconstruireTas(N[] tab,int debut, int fin)

    {

        int j;

        int fils;

         
        for(int i=fin-1;i>debut;i--)

        {  
            j=debut;

            echange(tab,debut,i);

            if(croissant==true)

            {    
                 
               
while(filsg(j) < i && tab[filsg(j)]<tab[j] || (filsd(j)
< i && tab[filsd(j)]<tab[j]))

                {

                    fils=filsg(j);

                     
                    if((filsd(j) < i && tab[filsd(j)]<tab[j]) && filsd(j) < i)

                    {

                        fils=filsd(j);

                    }                    

                    echange(tab,fils,j);

                     
                    j=fils;                    

                }

            }

            else

            {

                while(filsg(j) < i &&
tab[filsg(j)]<tab[j] || (filsd(j) < i &&
tab[filsd(j)]<tab[j]))

                {

                    fils=filsg(j);

                     
                    if((filsd(j) < i && tab[filsd(j)]<tab[j]) && filsd(j) < i)

                    {

                        fils=filsd(j);

                    }                    

                    echange(tab,fils,j);

                     
                    j=fils;                    

                }

            }

        }    
    }

 
    private void echange(N[] tab,int indice1, int indice2)

    {

        if(indice1 != indice2)

        {

            N tmp;

            tmp = tab[indice1];

            tab[indice1] = tab[indice2];

            tab[indice2] = tmp;

        }

    }

 
   

   /**

        calcul l'indice Nu se trNuve le pere du fils passe en parametre

          @param i indice du fils

     */

    private int pere(int i)

    {

        return (i+1)/2 - 1;

    }

 
    /**

        calcul l'indice Nu se trNuve le fils gauche du pere passe en parametre

          @param i indice du pere

     */

    private int filsg(int i)

    {

        return (2*i)+1;

    }

 
    /**

        calcul l'indice Nu se trNuve le fils drNit du pere passe en parametre

          @param i indice du pere

          @return  
     */

    private int filsd(int i)

    {

        return 2*(i+1);

    }

     
     
    public static void main(String[] a)

    {

       TrieurNb t =new TrieurNb (true);

       int [] tab = {1, 23, 3 ,12, 12};

       t.tri(tab,0,tab.length);

       for(int i = 0 ; i<tab.length;i++)

       {

         System.out.print(tab[i]+"\t" );

       }

       System.out.println();

    }

     
}

javac  Trieur
Nb.javaTrieurNb.java:5: > expected

public class TrieurNb<number N>

                            ^

TrieurNb.java:5: illegal start of type

public class TrieurNb<number N>

                              ^

TrieurNb.java:5: expected

public class TrieurNb<number N>

                               ^

TrieurNb.java:6: ';' expected

{

 ^

4 errors

7 réponses

Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
20 mars 2009 à 12:58
Merci de ne pas me MP pour ton code.
Je comprend bien que tu veux passer un tableau de int et non de Integer mais c'est simplement pas possible en java, tu dois passer par une classe wrapper quand tu manipules des templates.
Les templates java ne sont applicables qu'aux objets et non aux types primitifs donc tu n'as pas le choix.

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

OoWORAoO
3
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
20 mars 2009 à 11:14
Salut,

C'est ta déclaration qui est incorrecte :

public class TrieurNb<N<strike> number</strike>>
______________________________________
DarK Sidious
0
cs_chien57 Messages postés 17 Date d'inscription vendredi 16 mai 2008 Statut Membre Dernière intervention 22 avril 2009
20 mars 2009 à 11:46
alors comment fais tu pour les int , byte
0
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
20 mars 2009 à 11:51
Salut,

Ben il est où le problème ? C'est dans l'instanciation de ta classe que tu précise si tu veux des int, des bytes, etc., par exemple :

TrieurNb trieur = new TrieurNb(); // pour travailler avec des int
TrieurNb trieur = new TrieurNb(); // pour travailler avec des byte
______________________________________
DarK Sidious
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_chien57 Messages postés 17 Date d'inscription vendredi 16 mai 2008 Statut Membre Dernière intervention 22 avril 2009
20 mars 2009 à 12:00
quand je fais ce que dit [../auteur/DARKSIDIOUS/13557.aspx DARKSIDIOUS]

voila les erreur que j'ai:
TrieurNb.java:29: operator < cannot be applied to N,N
                while(j>=0 && pere(j)>=0 && tab[pere(j)]<tab[j])
                                                        ^
TrieurNb.java:37: operator < cannot be applied to N,N
                while(j>=0 && pere(j)>=0 && tab[pere(j)]<tab[j])
                                                        ^
TrieurNb.java:59: operator < cannot be applied to N,N
                while(filsg(j) < i && tab[filsg(j)]<tab[j] || (filsd(j) < i && tab[filsd(j)]<tab[j]))
                                                   ^
TrieurNb.java:59: operator < cannot be applied to N,N
                while(filsg(j) < i && tab[filsg(j)]<tab[j] || (filsd(j) < i && tab[filsd(j)]<tab[j]))
                                                                                            ^
TrieurNb.java:63: operator < cannot be applied to N,N
                    if((filsd(j) < i && tab[filsd(j)]<tab[j]) && filsd(j) < i)
                                                     ^
TrieurNb.java:74: operator < cannot be applied to N,N
                while(filsg(j) < i && tab[filsg(j)]<tab[j] || (filsd(j) < i && tab[filsd(j)]<tab[j]))
                                                   ^
TrieurNb.java:74: operator < cannot be applied to N,N
                while(filsg(j) < i && tab[filsg(j)]<tab[j] || (filsd(j) < i && tab[filsd(j)]<tab[j]))
                                                                                            ^
TrieurNb.java:78: operator < cannot be applied to N,N
                    if((filsd(j) < i && tab[filsd(j)]<tab[j]) && filsd(j) < i)
                                                     ^
TrieurNb.java:133: unexpected type
found   : int
required: reference
       TrieurNb t =new TrieurNb (true);
                ^
TrieurNb.java:133: unexpected type
found   : int
required: reference
       TrieurNb t =new TrieurNb (true);
                                     ^
10 errors
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
20 mars 2009 à 12:05
Salut,

les templates sur les  types primitifs (int, byte, char, long, float, short et double) ne fonctionne pas en java, il faut utiliser les classes wrapper (Integer, Byte, Character, Long, Float, Short et Double) après pour l'ajout ou la recup de types primitfs tu peux le faire simplement grâce à l'autoboxing.

ex :
List list = new ArrayList();
list.add(10);
int i = list.get(0);

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

OoWORAoO
0
cs_chien57 Messages postés 17 Date d'inscription vendredi 16 mai 2008 Statut Membre Dernière intervention 22 avril 2009
20 mars 2009 à 12:09
j'ai dejà fais un programme simmilaire pour les objet qui marche bien que voici

public class TrieurObj<O extends Comparable>
{
   private boolean croissant;

   /**
      construit un trieur de nombres
    */
   private TrieurObj()
   {
   }

   /**
      construit un trieur de nombres
    */
   public TrieurObj(boolean croissant)
   {
      this.croissant=croissant;
   }
  
  
    public void tri(O[] tab)
    {
        construireTas(tab,0,tab.length);
        deconstruireTas(tab,0,tab.length);
    }
   
    public void tri(O[] tab,int debut, int fin)
    {
        construireTas(tab,debut,fin);
        deconstruireTas(tab,debut,fin);
    }
  
    private void construireTas(O[] tab,int debut, int fin)
    {       
        int j;
       
        for(int i=debut+1;i<fin;i++)
        {
            j=i;
            if(croissant==true)
            {
                while(j>=0 && pere(j)>=0 && tab[pere(j)].compareTo(tab[j])<0)
                {
                    echange(tab,pere(j),j);
                    j=pere(j);
                }
            }
            else
            {
                while(j>=0 && pere(j)>=0 &&  tab[pere(j)].compareTo(tab[j])>0)
                {
                    echange(tab,pere(j),j);
                    j=pere(j);
                }
            }
        }
       
    }

    private void deconstruireTas(O[] tab,int debut, int fin)
    {
        int j;
        int fils;
       
        for(int i=fin-1;i>debut;i--)
        {
            j=debut;
            echange(tab,debut,i);
            if(croissant==true)
            {  
               
                while((filsg(j) < i && tab[filsg(j)].compareTo(tab[j])>0) || (filsd(j) < i && tab[filsg(j)].compareTo(tab[j])>0))
                {
                    fils=filsg(j);
                   
                    if(tab[filsd(j)].compareTo(tab[filsg(j)])>0 && filsd(j) < i)
                    {
                        fils=filsd(j);
                    }                   
                    echange(tab,fils,j);
                   
                    j=fils;                   
                }
            }
            else
            {
                while((filsg(j) < i && tab[filsg(j)].compareTo(tab[j])<0) || (filsd(j) < i && tab[filsg(j)].compareTo(tab[j])<0))
                {
                    fils=filsg(j);
                   
                    if(tab[filsd(j)].compareTo(tab[filsg(j)])<0 && filsd(j) < i)
                    {
                        fils=filsd(j);
                    }                   
                    echange(tab,fils,j);
                   
                    j=fils;                   
                }
            }
        }  
    }

    private void echange(O[] tab,int indice1, int indice2)
    {
        if(indice1 != indice2)
        {
            O tmp;
            tmp = tab[indice1];
            tab[indice1] = tab[indice2];
            tab[indice2] = tmp;
        }
    }

  
   /**
        calcul l'indice ou se trouve le pere du fils passe en parametre
          @param i indice du fils
     */
    private int pere(int i)
    {
        return (i+1)/2 - 1;
    }

    /**
        calcul l'indice ou se trouve le fils gauche du pere passe en parametre
          @param i indice du pere
     */
    private int filsg(int i)
    {
        return (2*i)+1;
    }

    /**
        calcul l'indice ou se trouve le fils droit du pere passe en parametre
          @param i indice du pere
          @return
     */
    private int filsd(int i)
    {
        return 2*(i+1);
    }
   
/**
    public static void main(String[] tab )
    {
       TrieurObj<String> t =new TrieurObj<String> (true);
       //int [] tab = {1, 23, 3 ,12, 12};
       t.tri(tab);
       for(int i = 0 ; i<tab.length;i++)
       {
         System.out.print(tab[i]+"\t");
       }
       System.out.println();
    }
*/
}
0
Rejoignez-nous