From 0baef0820510daaf07b2da4e0031e9ee98e19b90 Mon Sep 17 00:00:00 2001 From: Debucquoy Date: Thu, 27 Apr 2023 10:04:31 +0200 Subject: [PATCH] Adding Array Copy for matrix Currently only boolean but we can add more if we need --- .../main/java/school_project/Utils/Array.java | 13 ++++++++++ .../java/school_project/Utils/ArrayTest.java | 26 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 app/src/main/java/school_project/Utils/Array.java create mode 100644 app/src/test/java/school_project/Utils/ArrayTest.java diff --git a/app/src/main/java/school_project/Utils/Array.java b/app/src/main/java/school_project/Utils/Array.java new file mode 100644 index 0000000..9f722d6 --- /dev/null +++ b/app/src/main/java/school_project/Utils/Array.java @@ -0,0 +1,13 @@ +package school_project.Utils; + +import java.util.Arrays; + +public class Array{ + public static boolean[][] MatrixCopyOf(boolean[][] o){ + boolean[][] ret = new boolean[o.length][]; + for (int i = 0; i < o.length; i++){ + ret[i] = Arrays.copyOf(o[i], o[i].length); + } + return ret; + } +} diff --git a/app/src/test/java/school_project/Utils/ArrayTest.java b/app/src/test/java/school_project/Utils/ArrayTest.java new file mode 100644 index 0000000..70b66b9 --- /dev/null +++ b/app/src/test/java/school_project/Utils/ArrayTest.java @@ -0,0 +1,26 @@ +package school_project.Utils; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ArrayTest { + + @Test + void matrixCopyOf() { + boolean[][] a = new boolean[][] { + {true, false, true}, + {false, false, false}, + {true, false, true}, + }; + boolean[][] b = new boolean[][] { + {true, false, true}, + {false, false, false}, + {true, false, true}, + }; + boolean[][] c = Array.MatrixCopyOf(a); + assertArrayEquals(a, c); + a[1][1] = true; + assertArrayEquals(b, c); + } +} \ No newline at end of file -- 2.46.0