![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
[beginner] how to reverse the contents of a table
Hello,
I really am blocked now... I can't figure out the algorith to inverse the contects of a table... and I have been working on it for quite a while now but I don't get decent results.. here's my code class Table{
int contenu[];
int taille;
Table(){
System.out.println("quelle est la taille du tableau à créer");
taille = Clavier.lireInt();
contenu = new int[taille];
initValue();
}
Table(int n){
taille = n;
contenu = new int[taille];
initValue();
}
private void initValue(){
System.out.println("valeur de chacune des cases");
for(int i=0;i<taille;i++){
contenu[i]= Clavier.lireInt();
}
}
// fonction d'affichage
public String toString(){
String s="[";
for(int i=0;i<taille-1;i++)
s=s+contenu[i]+",";
s=s+contenu[taille-1]+"]";
return(s);
}
public void reverse(){
int tab2[]= new int[taille];
for(int i = taille-1; i>=0;i--){
int k =0 ;
tab2[k] = contenu[i];
k++;
}
for (int i=0;i<taille;i++){
contenu[i]= tab2[i];
}
}
public void sort(){
int j;
for(int i=1;i<taille;i++){
for(j=0;contenu[j]<contenu[i];j++);
int tampon = contenu[i];
for (int k=i;k>j;k--)
contenu[k] = contenu[k-1];
contenu[j] = tampon;
}
}
}
public class Tableau{
public static void main(String args[]){
Table tab = new Table(5);
tab.sort();
System.out.println( "tableau trié " + tab);
Table tab1 = new Table();
System.out.println("tableau non inverse " +tab1);
tab1.reverse();
System.out.println("tableau inversé " +tab1);
}
}thanks in advance, Mario
__________________
printf("\n I might need you to help me tonight"); getch(); |
|
|
|
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|