IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

Matrice à plusieurs variables


Sujet :

Java

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2013
    Messages
    28
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2013
    Messages : 28
    Points : 23
    Points
    23
    Par défaut Matrice à plusieurs variables
    bonjour je crée une matrice M(ligne i,Col j)
    et la valeur value à la position i et j.
    si value es t un scalaire de type int ou long ca marche mais je voudrais que
    value soit un tableau de 3 éléments.
    si je compile se code j ai un résultat de


    matrice a :

    [
    0 1 1 0 0
    1 0 1 1 1
    1 1 0 0 1
    0 1 0 0 0
    0 1 0 0 1
    ]
    mais moi je voudrai obtenir

    matrice a :

    [
    0,0,0 1,0,1 0,0,0 0,0,0
    0,0,0 1,0,1 0,0,0 0,0,0
    0,0,0 1,0,1 0,0,0 0,0,0
    0,0,0 1,0,1 0,0,0 0,0,0
    ]
    j'ai besoin d'aide merci de votre compréhension.


    ///////////////////////////////////////////////////////////////////////////////////////////////////////////

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    package MagicMatrix;
     
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Hashtable;
     
    import com.lowagie.text.pdf.codec.Base64.OutputStream;
     
    import ucar.nc2.NCdump;
    import ucar.nc2.NetcdfFile;
     
     
    public class MatriceMGM {
    	private long[][] coeff = null;
     
    	/**
             * Constructeur Matrice
             * 
             * @param int i - ligne int j - colonne
             */
    	public MatriceMGM(int i, int j) {
    		this.setLength(i, j);
    	}
     
    	public MatriceMGM() {
    		this(0, 0);
    	}
     
    	public MatriceMGM(long[][] mat) {
    		this.coeff = mat;
    	}
     
    	// définit une matrice de type long[][]
    	public void setMatrice(long[][] mat) {
    		this.coeff = mat;
    	}
     
    	// définit une valeur à la position i et j
    	// i - ligne
    	// j - col
    	public void setValue(int i, int j, long value) {
     
    		this.coeff[i][j] = value;
    	}
    /**la question est: comment faire pour que
            le resultat de coeff[i][j] le soit pas un scalaire mais un tableau de 3 elts cad
            coeff[i][j]=(0,1,0) par exemple
            **/
     
     
     
    //	public void setValue(int i, int j, long[] value) {
    //		value=new long[3]
    //		
    //		this.coeff[i][j] = value[i];
    //	}
     
     
    	// on définit la taille de la matrice
    	public void setLength(int i, int j) {
    		this.coeff = new long[i][j];
    	}
     
    	// retourne la matrice sous forme du type long[][]
    	public long[][] getMatrice() {
    		return this.coeff;
    	}
     
    	// retourne le nombre de ligne
    	public int getRows() {
    		return this.coeff.length;
    	}
     
    	// retourne le nombre de colonne
    	public int getColumns() {
    		return this.coeff[0].length;
    	}
     
    	// retourne la valeur à la position i et j
    	public long getValue(int i, int j) {
    		return this.coeff[i][j];
    	}
     
    	public String toString() {
    		String out = "\n[\n";
     
    		for (int i = 0; i < this.getRows(); i++) {
    			for (int j = 0; j < this.getColumns(); j++)
    				out += this.coeff[i][j] + "\t";
     
    			out += "\n";
    		}
     
    		return out + "]";
    	}
     
    	public static void main(String[] args) {
    		// matrice mgm
    		MatriceMGM a = new MatriceMGM();
    		a.setMatrice(new long[][] { { 0, 1, 1, 0, 0 }, // 1
    				{ 1, 0, 1, 1, 1, }, // 2
    				{ 1, 1, 0, 0, 1, }, // 3
    				{ 0, 1, 0, 0, 0, }, // 4
    				{ 0, 1, 0, 0, 1, },// 5
    		});
    		System.out.println("matrice a :\n " + a);
    		MatriceMGM x = new MatriceMGM(new long[][] { { 0, 1, 0, 0 },
    				{ 1, 0, 0, 0 }, { 0, 0, 1, 1 }, { 0, 0, 0, 1 } });
     
    		MatriceMGM mgm = new MatriceMGM();
    		// taille mat
    		mgm.setLength(10, 10);
    		//System.out.println("taille mat " + mgm);
    		// remplissage de la matrice
    		// for (int i = 0; i < 10; i++) {
    		// for (int j = 0; j < 10; j++) {
    		// int ligne = i;
    		// System.out.println("val " + ligne);
    		//
    		// int col = j;
    		//
    		// long[] v = { 0, 0, 1 };
    		// mgm.setValue(ligne, col, v);
    		// }
    		// }
    		//
    		// System.out.println("mgm \n " + mgm.toString());
     
    	}
     
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////

  2. #2
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 074
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 074
    Points : 7 978
    Points
    7 978
    Par défaut
    Au lieu d'avoir long [][] tu peux très bien avoir long [3][][] ou encore long[][][3] ou utiliser une classe TUPLE (crée par toi) contenant 3 variables (ou un tableau de 3 elements) et tu auras donc TUPLE[][] au lien de long [][].
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

Discussions similaires

  1. Réponses: 6
    Dernier message: 08/04/2010, 13h13
  2. Plusieurs variables dans Dim et Case??
    Par samlepiratepaddy dans le forum Access
    Réponses: 2
    Dernier message: 02/10/2005, 18h04
  3. Envoyer plusieur variable a un programme
    Par scaleo dans le forum Langage
    Réponses: 9
    Dernier message: 06/09/2005, 11h09
  4. envoyer plusieurs variables dans l'url?
    Par brgui dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 18/08/2005, 14h18
  5. Réponses: 5
    Dernier message: 28/04/2004, 16h06

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo