Tableau en java

bassma -  
 Utilisateur anonyme -
Bonjour,
je veux crée un tableau a 2 dimensions et il va remplir par l'utilisateur,commet?

1 réponse

  1. Utilisateur anonyme
     
    Voilà un exemple fonctionnel :

    import java.util.Scanner;
    
    public class LauncherTwoDimArrayInput {
        private int[][] data;
        private int rowMax;
        private int colMax;
    
        public LauncherTwoDimArrayInput(int row, int col) {
            this.rowMax = row;
            this.colMax = col;
            this.data = new int[this.rowMax][this.colMax];
        }
    
        public void fill() {
            Scanner in = new Scanner(System.in);
            for (int row = 0; row < this.data.length; row++) {
                for (int col = 0; col < this.data[row].length; col++) {
                    System.out.println("Entrer un entier :");
                    this.data[row][col] = in.nextInt();
                }
                System.out.println();
            }
        }
    
        @Override
        public String toString() {
            String ret = "";
    
            for (int row = 0; row < this.data.length; row++) {
                for (int col = 0; col < this.data[row].length; col++) {
                    ret += data[row][col];
                }
                ret += '\n';
            }
    
            return ret;
        }
    
        public static void main(String[] args) {
            LauncherTwoDimArrayInput launcherTwoDimArrayInput = new LauncherTwoDimArrayInput(2, 3);
            launcherTwoDimArrayInput.fill();
            System.out.println(launcherTwoDimArrayInput.toString());
        }
    }
    
    
    2