Tableau java

Salut les gens,

Voila je vous explique mon probleme :

J'ai crée un tableau en java
static String TableauBallon[][] = new String [10][2];

et je veut que l'utilisateur de ce prog est la possibilité de supprimer une occurance de ce tableau j'ai testé

TableauBallon[x][y]= null;
TableauBallon[x][y]= void;

mais bien sur cela ne fonctionne pas ^^

donc voila si vous avez une idée, je suis preneur je met mon script complet en dessous :

public class essai {

static int i = 0;
static int j = 0;

public String identificateur; // identifier un ballon
public int taille; // taille du ballon
public void Ballon(String z, int t) {} // constructeur
public String toString() {
return identificateur;} // l’instance en string

static String TableauBallon[][] = new String [10][2];

public static void main (String args[]){

int s = 1;

while ( s!=0){

System.out.println("+++++++++++++(MENU)+++++++++++++");
System.out.println("1) Créer un ballon");
System.out.println("2) Détruire un ballon");
System.out.println("3) Afficher la liste des ballons");
System.out.println("0) Quitter");
System.out.println("++++++++++++++++++++++++++++++++");
System.out.println("");
System.out.println("(Veuillez faire votre choix)");

s = Keyboard.readInt();

switch (s) {

case 1 :
creer_instance();
break;

case 2 :
detruire_element_instance();
break;

case 3 :
afficher_instance();
break;

case 0 :
System.out.println("Au revoir !");
System.exit(0);

break;

}

}

}
private static void detruire_element_instance() {
System.out.println("Quel ballon souhaiter vous supprimer ?");
String detruire = Keyboard.readString();
for (int x = 0 ; x < 10 ; x++ ){
for (int y = 0 ; y < 2 ; y++ ){

if(TableauBallon[x][y] == detruire){

TableauBallon[x][y]= "null";
System.out.println("Le ballon "+detruire+" à bien été supprimer");

}

}
}

}
private static void afficher_instance() {

for (int x = 0 ; x < 10 ; x++ ){
for (int y = 0 ; y < 2 ; y++ )
System.out.println(TableauBallon[x][y]);
}
}
private static void creer_instance() {
System.out.println("Tapez un indentifiant pour votre ballon :");
String identifiant = Keyboard.readString();
System.out.println(identifiant);
TableauBallon [i][j] = identifiant;
j=j+1;
System.out.println("Tapez une taille pour votre ballon :");
int t = Keyboard.readInt();
String taille=Integer.toString(t);
System.out.println(taille);
TableauBallon [i][j] = taille;
i=i+1;
j=j-1;

}

}

--
Tout ce que je sais, c'est que je ne sais rien.
Configuration: Windows 2000
Firefox 2.0.0.2

8 réponses

  1. Hello,

    J'ai regarder ton code et première remarque que j'aurais à te faire, serait de créer une class Ballon

    public class Ballon() {

    private String identificateur; // identifier un ballon
    private int taille; // taille du ballon

    // constructeur
    public Ballon(String identificateur, int taille) {

    this.identificateur=identificateur ;
    this.taille=taille ;

    }

    //il faudra créer les getter/setter.

    }

    et ensuite dans la class essai du construit un tableau de ballon :

    Ballon TableauBallon[][] = new Ballon[10][2]

    méthode détruire :

    private static void detruire_element_instance() {

    System.out.println("Quel ballon souhaitez-vous supprimer ?");

    System.out.println("ligne du ballon : ");
    int i = Integer.parseInt(Keyboard.readString());

    System.out.println("colone du ballon :");
    int j = Integer.parseInt(Keyboard.readString());

    if(TableauBallon[i][j] == null){

    System.out.println("Le ballon n'existe pas");

    }
    else{
    System.out.println("Le ballon a ete supprime");
    }
    }

    méthode afficher :

    private static void afficher_instance() {

    for (int x = 0 ; x < tableau.length ; x++ ){
    for (int y = 0 ; y < tableau[0].length ; y++ )
    System.out.println(TableauBallon[x][y]);
    }

    méthode créer un ballon :

    private static void creer_instance() {
    System.out.println("Tapez les donnees du ballon :");
    System.out.println("ligne du ballon : ");
    int i = Integer.parseInt(Keyboard.readString());

    System.out.println("colone du ballon :");
    int j = Integer.parseInt(Keyboard.readString());

    System.out.println("identificateur du ballon :");
    String id = Keyboard.readString();

    System.out.println("taille du ballon :");
    int dim = Integer.parseInt(Keyboard.readString());

    // il faut encore tester que le ballon ne soit pas créer endehors du tableau.

    TableauBallon[i][j] = new Ballon(id, dim) ;

    }
    1. Je comprend pas avec quel ligne de code tu supprime un élément de mon tableau
      1. Oups, g oublié :

        if(TableauBallon[i][j] == null){

        System.out.println("Le ballon n'existe pas");

        }
        else{
        TableauBallon[i][j] = null ;
        System.out.println("Le ballon a ete supprime");
        }
        1. Bonjour
          Je cherche cmt je peux afficher les éléments de mon tableau. Est ce que il y a personne qui connait le faire ?
          voila le code de création de mon tableau :
          ---------------------------------------------------------------
          public static void main (String args [])
          {int tab[];
          tab = new int[4];
          tab[0]=5;
          tab[1]=3;
          tab[2]=7;
          tab[3]=tab[1]+ tab[2];
          }
          ------------------------------------------------------------------
          Merci en avance!!!
          1. Hello Iroiim,

            en faisant une boucle tu pourras afficher chaque élément de ton tableau.

            for (int i=0; i<tab.length; i++)
                  System.out.println(tab[i]+"");
            
          2. c'est facile pour afficher les éléments , vous devez rajouter cette partie:

            for(int i=0;i<4;i++)
            System.out.println(tab[i]);
        2. version 03/2010 du tableau de ballon
          source 1

          public class Ballon {

          // init des attributs

          private String identificateur; // identifier un ballon
          private int taille; // taille du ballon

          // constructeur methode

          //constructeur methode sans parametres

          public Ballon() {
          }

          //constructeur surxhage avec parametres

          public Ballon(String identificateur, int taille) {

          this.identificateur=identificateur ;
          this.taille=taille ;
          }

          public String getIdentificateur() {
          return this.identificateur;
          }

          public void setIdentificateur(String identificateur) {
          this.identificateur = identificateur;
          }
          public int getTaille() {
          return this.taille;
          }

          public void setTaille(int taille) {
          this.taille = taille;
          }

          public boolean equals(Object other) {
          if ((this == other))
          return true;
          if ((other == null))
          return false;
          if (!(other instanceof Ballon))
          return false;

          Ballon castOther = (Ballon) other;

          return (this.getIdentificateur() == castOther.getIdentificateur())

          && (this.getTaille() == castOther.getTaille());

          }

          }
          ///////////////////////////////////////////////////////

          source 2

          import java.util.Scanner;

          public class ballonEssai {

          static Ballon TableauBallon[][] = new Ballon[40][40];

          private static void detruire_element_instance() {
          Scanner sc = new Scanner(System.in);
          System.out.println("Quel ballon souhaitez-vous supprimer ?");

          System.out.println("ligne du ballon : ");

          String str4 = sc.nextLine();
          System.out.println("Vous avez saisi : " + str4);

          int i = Integer.parseInt(str4);

          System.out.println("colone du ballon :");
          String str5 = sc.nextLine();
          System.out.println("Vous avez saisi : " + str5);

          int j = Integer.parseInt(str5);

          if(TableauBallon[i][j] == null){

          System.out.println("Le ballon n'existe pas");

          }
          else{
          TableauBallon[i][j] = null ;
          System.out.println("Le ballon a ete supprime");
          }
          }

          private static void afficher_instance() {

          for (int x = 0 ; x < TableauBallon.length ; x++ ){

          for (int y = 0 ; y < TableauBallon[0].length ; y++ )

          { try
          {if (!(TableauBallon[x][y].getIdentificateur()== null)|
          !(TableauBallon[x][y].getTaille()== 0))
          System.out.println("ballon x>"+x+"ballon y>"+y
          +"<id>"+ TableauBallon[x][y].getIdentificateur()
          +"<dim>"+ TableauBallon[x][y].getTaille());
          }
          catch (Exception e) {
          System.out.println("ballon x>"+x+"ballon y>"+y+"*****aucun ballon à afficher ");}

          }
          }
          }
          //////////////////////////////////////////////////////////
          private static void creer_instance() {
          System.out.println("Tapez les donnees du ballon :");
          System.out.println("ligne du ballon : ");
          Scanner sc = new Scanner(System.in);

          String str1 = sc.nextLine();
          System.out.println("Vous avez saisi : " + str1);

          int i = Integer.parseInt(str1);

          System.out.println("colone du ballon :");

          String str2 = sc.nextLine();
          System.out.println("Vous avez saisi : " + str2);

          int j = Integer.parseInt(str2);

          System.out.println("identificateur du ballon :");
          String id = sc.nextLine();
          System.out.println("Vous avez saisi : " + id);

          System.out.println("taille du ballon :");
          String str3 = sc.nextLine();
          System.out.println("Vous avez saisi : " + str3);

          int dim = Integer.parseInt(str3);

          // il faut encore tester que le ballon ne soit pas créer endehors du tableau.
          System.out.println("i>"+i+"<j>"+j+"<id>"+id+"<dim>"+dim);
          TableauBallon[i][j] = new Ballon(id, dim) ;

          }
          public static void main (String args[]){
          Scanner sc = new Scanner(System.in);
          int s = 1;

          while ( s!=0){

          System.out.println("+++++++++++++(MENU)+++++++++++++");
          System.out.println("1) Créer un ballon");
          System.out.println("2) Détruire un ballon");
          System.out.println("3) Afficher la liste des ballons");
          System.out.println("0) Quitter");
          System.out.println("++++++++++++++++++++++++++++++++");
          System.out.println("");
          System.out.println("(Veuillez faire votre choix)");

          String str1 = sc.nextLine();
          System.out.println("Vous avez saisi : " + str1);

          s= Integer.parseInt(str1);

          switch (s) {

          case 1 :
          creer_instance();
          break;

          case 2 :
          detruire_element_instance();
          break;

          case 3 :
          afficher_instance();
          break;

          case 0 :
          System.out.println("Au revoir !");
          System.exit(0);

          break;

          }

          }

          }

          }
          1. Bonjour,
            afficher dans un tableau java 10 entier et afficher le plus grand element et son indice.
            merci davance
            1. bonjour
              je veux creer un tableau qui contient n elements et dont les valeurs seront saisies par l'utilisateur . je vous demande un coup de pouce
              int tableau =new int [n]
              int n
              je ne sais pas si cette declaration est juste
              j'ai un probleme avec la declarartion car la taille du tableau est une variable
              Merci et bonne journee
              1. -=(L)=-, tu peux aussi utiliser un vecteur.
                Quand ta méthode supprimera une des "cases", ca fera le lien derrière automatiquement. (comme les pointeurs sous pascal)