ce compile le code ci-dessous aucune erreur mais à l'exécution j'ai le
message
E:\divers\ets\LOG\log320\aut03\lab1>java Sort
Exception in thread "main" java.lang.ClassCastException
at Sort.radixSort(Sort.java:30)
at Sort.statistique(Sort.java:101)
at Sort.main(Sort.java:139)
Long[]a;
long baro=0;
long[][] tabStat= new long[50][6];
int maxTest=50;
//algo du tri radixSort
public void radixSort( Long a[], int k, int log) {
LinkedList[] list = new LinkedList[ log ];
for( int i = 0; i < log; i++ ){
list[ i ] = new LinkedList( );
baro++;
}
LinkedList tmpList = new LinkedList( );
for( int i = 0; i < a.length; i++ ){
tmpList.addLast( a[ i ] );
baro++;
}
for( int i = 0, mult = 1; i < k; i++ ) {
while( !tmpList.isEmpty( ) ) { //ligne 30
Integer p = (Integer)tmpList.removeFirst( );
list[ ( p.intValue( ) / mult ) % log ].addLast( p );
baro++;
}
for( int j = 0; j < log; j++ ) {
tmpList.addAll( list[ j ] );
list[ j ].clear( );
baro++;
}
mult *= log;
}
tmpList.toArray( a );
}
//ecrire les valeurs du tableau dans le tableau
public void ecrireFichier(Long a[], String NomFichier){
try{
PrintWriter out = new PrintWriter(new FileWriter(NomFichier));
for (int i = 0; i < a.length; i++)
out.println(a[i]);
out.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//ecrire les valeurs des barometre
public void ecrireBaro(long[][] tabStat, String NomFichier){
try{
PrintWriter out = new PrintWriter(new FileWriter(NomFichier));
for(int i=0;i<maxTest;i++){
out.println(tabStat[i][0] );
out.println(tabStat[i][1] );
out.println(tabStat[i][2] );
out.println(tabStat[i][3] );
out.println(tabStat[i][4] );
out.println(tabStat[i][5] );
out.println("=============" );
out.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
//genenerer les valeurs pour le tableau
public void genererTableau(Long a[]){
long rang=4294967296l; //rang
Random r1 = new Random();
for (int i=0;i<a.length;i++){
a[i] = new Long ( (long) (r1.nextDouble()*rang));
}
}
public void statistique(){
int n=100; //nombre d'élément du tableau
a = new Long[n];
Long []original;
original = new Long[n];
for(int i=0;i<maxTest;i++){
genererTableau(a);
original=a;
//base 2 - passe k
radixSort( a, 32,2 ); //ligne 101
tabStat[i][0]=baro;
//base 8 - passe k/3
a=original;
baro=0;
radixSort( a, 32/3,8 );
tabStat[i][1]=baro;
//base 16 - passe k/4
a=original;
baro=0;
radixSort( a, 32/4,16 );
tabStat[i][2]=baro;
//base 64 - passe k/6
a=original;
baro=0;
radixSort( a, 32/6,64 );
tabStat[i][3]=baro;
//base racine(n) - passe k/log2(racine(n))
a=original;
baro=0;
radixSort( a, (int)(Math.sqrt(n)),
(int)(32/(Math.log(Math.sqrt(n))/Math.log(2.0))) );
tabStat[i][5]=baro;
//base racine 3ieme(n) - passe k/log2(racine 3ieme(n))
a=original;
baro=0;
radixSort( a, (int)(Math.pow(3,1/n)),
(int)(32/(Math.log(Math.pow(3,1/n))/Math.log(2.0))) );
tabStat[i][6]=baro;
}
ecrireBaro(tabStat,"stat.txt");
}
public static void main( String[] args ) {
Sort tmp = new Sort();
tmp.statistique(); //ligne 139
}
Normal, tmpList est remplie de Long et tu castes vers un Integer. D'où une ClassCastException.
Soit tu remplis ta liste d'Integer, soit tu utilises des Long dans ton code, mais pas un mélange des deux.
En passant, tu devrais peut-être songé à utiliser des tableaux de int ou long plutôt que des Objets couteux. -- Olivier
en changeant en Long, j'obtient les mêmes erreurs de compilation...
si je change ca en long.. il faut que je modifie assez de stock -- Borland rulez http://pages.infinit.net/borland
os2
Olivier Thomann wrote:
On Fri, 31 Oct 2003 14:20:47 -0500, os2 wrote:
Integer p = (Integer)tmpList.removeFirst( );
Normal, tmpList est remplie de Long et tu castes vers un Integer. D'où une ClassCastException.
Soit tu remplis ta liste d'Integer, soit tu utilises des Long dans ton code, mais pas un mélange des deux.
En passant, tu devrais peut-être songé à utiliser des tableaux de int ou long plutôt que des Objets couteux. -- Olivier en changeant en Long, j'obtient les mêmes erreurs de compilation...
Longp = (Long)tmpList.removeFirst( );
si je change ca en long.. il faut que je modifie assez de stock
-- Borland rulez http://pages.infinit.net/borland
Olivier Thomann wrote:
On Fri, 31 Oct 2003 14:20:47 -0500, os2 <os2l@nospamvideotron.ca>
wrote:
Integer p = (Integer)tmpList.removeFirst( );
Normal, tmpList est remplie de Long et tu castes vers un Integer. D'où
une ClassCastException.
Soit tu remplis ta liste d'Integer, soit tu utilises des Long dans ton
code, mais pas un mélange des deux.
En passant, tu devrais peut-être songé à utiliser des tableaux de int
ou long plutôt que des Objets couteux.
--
Olivier
en changeant en Long, j'obtient les mêmes erreurs de compilation...
Longp = (Long)tmpList.removeFirst( );
si je change ca en long.. il faut que je modifie assez de stock
Normal, tmpList est remplie de Long et tu castes vers un Integer. D'où une ClassCastException.
Soit tu remplis ta liste d'Integer, soit tu utilises des Long dans ton code, mais pas un mélange des deux.
En passant, tu devrais peut-être songé à utiliser des tableaux de int ou long plutôt que des Objets couteux. -- Olivier en changeant en Long, j'obtient les mêmes erreurs de compilation...
Longp = (Long)tmpList.removeFirst( );
si je change ca en long.. il faut que je modifie assez de stock