From a5bf0d6f882ac9fc52540c3c54645ce2bcfc0068 Mon Sep 17 00:00:00 2001 From: Yanis O <oualanyanis01@gmail.com> Date: Wed, 13 Nov 2024 12:57:32 +0100 Subject: [PATCH] =?UTF-8?q?[Modif]=20Changement=20de=20Matrix,=20afin=20de?= =?UTF-8?q?=20supporter=20les=20matrices=20non=20carr=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/util/Matrix.java | 75 +++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/src/main/java/util/Matrix.java b/src/main/java/util/Matrix.java index 3e4cb8e..38c6109 100644 --- a/src/main/java/util/Matrix.java +++ b/src/main/java/util/Matrix.java @@ -6,27 +6,90 @@ import java.util.NoSuchElementException; public class Matrix<E> implements Iterable<E> { private ArrayList<ArrayList<E>> matrix; + private final int rows; + private final int columns; - public Matrix() { - this.matrix = new ArrayList<ArrayList<E>>(); + public Matrix(int rows, int columns) { + this.rows = rows; + this.columns = columns; + this.matrix = new ArrayList<>(rows); + + // Initialiser chaque ligne de la matrice + for (int i = 0; i < rows; i++) { + ArrayList<E> row = new ArrayList<>(columns); + // Initialiser chaque colonne avec des valeurs nulles + for (int j = 0; j < columns; j++) { + row.add(null); + } + this.matrix.add(row); + } } public E get(int x, int y) { + validateIndexes(x, y); return matrix.get(x).get(y); } public E set(int x, int y, E object) { + validateIndexes(x, y); return matrix.get(x).set(y, object); } public void clear() { - this.matrix = new ArrayList<ArrayList<E>>(); + this.matrix.clear(); + for (int i = 0; i < rows; i++) { + ArrayList<E> row = new ArrayList<>(columns); + for (int j = 0; j < columns; j++) { + row.add(null); + } + this.matrix.add(row); + } } public int size() { - return matrix != null ? matrix.get(0).size() * matrix.size() : 0; + return rows * columns; } + public int getColumns(){ + return this.columns; + } + public int getRows(){ + return this.rows; + } + public void displayMatrix() { + System.out.print(" "); + for (int j = 0; j < columns; j++) { + System.out.print("___ "); + } + System.out.println(); + + for (int i = 0; i < rows; i++) { + System.out.print("| "); + for (int j = 0; j < columns; j++) { + if (matrix.get(i).get(j) != null) { + System.out.print(" x | "); + } else { + System.out.print(" | "); + } + } + System.out.println(); + System.out.print(" "); + for (int j = 0; j < columns; j++) { + System.out.print("___ "); + } + System.out.println(); + } + } + + private void validateIndexes(int x, int y) { + if (x < 0 || x >= rows || y < 0 || y >= columns) { + throw new IndexOutOfBoundsException("Indices x: "+ x + " y: " + y + " hors limites pour la matrice."); + } + } + + + + @Override public Iterator<E> iterator() { return new MatrixIterator(); @@ -38,7 +101,7 @@ public class Matrix<E> implements Iterable<E> { @Override public boolean hasNext() { - return row < matrix.size() && col < matrix.get(row).size(); + return row < rows && col < columns; } @Override @@ -48,7 +111,7 @@ public class Matrix<E> implements Iterable<E> { } E element = matrix.get(row).get(col); col++; - if (col >= matrix.get(row).size()) { + if (col >= columns) { col = 0; row++; } -- GitLab