Matrice Sur Java
abdeloo
Messages postés
11
Date d'inscription
Statut
Membre
Dernière intervention
-
KX Messages postés 16761 Date d'inscription Statut Modérateur Dernière intervention -
KX Messages postés 16761 Date d'inscription Statut Modérateur Dernière intervention -
Salut Tout le monde j'ai un problème avec mon Programme Java qui concerne d'écrire une classe définissant le concept de matrice carré avec un constructeur à un seul argument.
Mais a ce ligne r[i][j]= m[i][j] + k[i][j]; il m'a donner un erreur : The type of the expression must be an array type but it resolved to Matrice
public class Matrice { private double m[][]; public Matrice(double m[][]) { this.m = m; } public void multiplie(double n) { for(int i=0;i<m.length;i++) { for(int j=0;j<m[i].length;j++) { m[i][j]*=n; } } } public Matrice Somme(Matrice k) { Matrice r = null; for(int i=0;i<m.length;i++) { for(int j=0;j<m[i].length;j++) { r[i][j]= m[i][j] + k[i][j]; } } return r; } }
Mais a ce ligne r[i][j]= m[i][j] + k[i][j]; il m'a donner un erreur : The type of the expression must be an array type but it resolved to Matrice
A voir également:
- Afficher matrice java
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Jeux java itel 5360 - Forum Mobile
- Eclipse java - Télécharger - Langages
- Java apk - Télécharger - Langages
2 réponses
Bonjour,
Tu ne devrais pas manipuler directement les tableaux, et encore moins le passer en constructeur de ta classe, il faut travailler sur l'objet Matrice que tu manipules, pas sur ce qui la constitue.
Exemple :
Tu ne devrais pas manipuler directement les tableaux, et encore moins le passer en constructeur de ta classe, il faut travailler sur l'objet Matrice que tu manipules, pas sur ce qui la constitue.
Exemple :
import java.util.function.BiFunction; public class Matrix { private final int width; private final int height; private final double[][] datas; public Matrix(int width, int height) { this.width = width; this.height = height; this.datas = new double[width][height]; } public void setData(int i, int j, double data) { datas[i][j] = data; } public double getData(int i, int j) { return datas[i][j]; } public int getWidth() { return width; } public int getHeight() { return height; } public static Matrix assignAll(Matrix matrix, BiFunction<Integer, Integer, Double> assignation) { for (int i = 0; i < matrix.getWidth(); i++) { for (int j = 0; j < matrix.getHeight(); j++) { matrix.setData(i, j, assignation.apply(i, j)); } } return matrix; } public static Matrix product(Matrix matrix, double n) { return assignAll(new Matrix(matrix.getWidth(), matrix.getHeight()), (i, j) -> matrix.getData(i, j) * n); } public static Matrix sum(Matrix matrix1, Matrix matrix2) { return assignAll(new Matrix(matrix1.getWidth(), matrix1.getHeight()), (i, j) -> matrix1.getData(i, j) + matrix2.getData(i, j)); } }