From a3560d69423e086ab2f296b0163172a1286ceed4 Mon Sep 17 00:00:00 2001 From: Ryuu Mitsuki Date: Fri, 13 Sep 2024 15:36:39 +0700 Subject: [PATCH] feat: Add support for negative indices in `display` methods * Enhanced the `display(int index)` method in the `Matrix` class to support negative indices. Negative indices are now interpreted as counting from the end of the matrix. For instance, an index of `-1` refers to the last row, `-2` refers to the second-to-last row, and so on. * Adjusted the index calculation to convert negative indices into valid positive indices. * Added checks to ensure that the adjusted index is within valid bounds for the matrix rows. * Improved error handling to raise an `InvalidIndexException` with a clear message if the adjusted index is out of bounds. * Updated the static `display(double[][] arr, int index)` method to support negative indices similarly. These updates provide greater flexibility for accessing rows in matrices and 2D arrays using negative indices, aligning with common conventions and improving usability. --- src/main/java/com/mitsuki/jmatrix/Matrix.java | 52 ++++++++----------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/mitsuki/jmatrix/Matrix.java b/src/main/java/com/mitsuki/jmatrix/Matrix.java index 8b18266..62d076f 100644 --- a/src/main/java/com/mitsuki/jmatrix/Matrix.java +++ b/src/main/java/com/mitsuki/jmatrix/Matrix.java @@ -5237,25 +5237,20 @@ final public void display() { * @see #getEntries() */ final public void display(int index) { - if (this.ENTRIES != null) { - // Check for negative index and throw the exception - if (index < 0) { - cause = new InvalidIndexException( - "Invalid given index. Index cannot be a negative value."); - } - // Check if the given index greater than number of rows this matrix - else if (index > this.ROWS - 1) { - cause = new InvalidIndexException( - "Invalid given index. Index cannot be larger than number of rows."); - } - - // Throw the exception if got one - if (cause != null) raise(cause); - - System.out.println(Arrays.toString(this.ENTRIES[index])); - } else { + if (this.ENTRIES == null) { System.out.println(""); + return; + } + + int rows = this.getNumRows(); + index += (index < 0) ? rows: 0; // Support negative index + if (index < 0 || index >= rows) { + raise(new InvalidIndexException("Given row index is out of bounds: " + + ((index < 0) ? (index - rows) : index) + )); } + + System.out.println(Arrays.toString(this.ENTRIES[index])); } @@ -5312,21 +5307,18 @@ final public static void display(double[ ][ ] arr) { final public static void display(double[ ][ ] arr, int index) { if (arr == null || arr.length == 0) { System.out.println(""); - } else { - // Checking index value - if (index < 0) { - cause = new InvalidIndexException( - "Invalid given index. Index cannot be a negative value."); - } else if (index > arr.length - 1) { - cause = new InvalidIndexException( - "Invalid given index. Index cannot be larger than number of rows."); - } - - // Throw the exception if got one - if (cause != null) raise(cause); + return; + } - System.out.println(Arrays.toString(arr[index])); + int rows = arr.length; + index += (index < 0) ? rows: 0; // Support negative index + if (index < 0 || index >= rows) { + raise(new InvalidIndexException("Given row index is out of bounds: " + + ((index < 0) ? (index - rows) : index) + )); } + + System.out.println(Arrays.toString(arr[index])); }