diff --git a/modules/sasha/src/main/java/com/hillel/elementary/javageeks/mycollection/MyList.java b/modules/sasha/src/main/java/com/hillel/elementary/javageeks/mycollection/MyList.java new file mode 100644 index 0000000..b011e58 --- /dev/null +++ b/modules/sasha/src/main/java/com/hillel/elementary/javageeks/mycollection/MyList.java @@ -0,0 +1,53 @@ +package com.hillel.elementary.javageeks.mycollection; + +import java.util.LinkedList; + +public class MyList { + + public static LinkedList actMatrix(int[][] arrOne,int[][] arrTwo,String argument){ + LinkedList listFist = matrixToList(arrOne); + LinkedList listSecond = matrixToList(arrTwo); + + LinkedList listResult = actList(listFist, listSecond,argument); + return listResult; + + } + + private static LinkedList matrixToList(int[][] arr){ + LinkedList list = new LinkedList(); + for (int i = 0;i < arr.length;i++){ + for (int k = 0;k < arr[0].length;k++){ + if(arr[i][k] != 0){ + list.add(arr[i][k]); + } + } + } + return list; + } + + private static LinkedList actList(LinkedList listOne,LinkedList listTwo,String argument){ + LinkedList list = new LinkedList(); + int minSize = 0; + if(listOne.size() <= listTwo.size()){ + minSize = listOne.size(); + } + else { + minSize = listTwo.size(); + } + + if(argument == "sum") { + for (int i = 0; i < minSize; i++) { + list.add(listOne.get(i) + listTwo.get(i)); + } + return list; + } + if(argument == "multiply"){ + for(int i = 0;i < minSize;i++){ + list.add(listOne.get(i) * listTwo.get(i)); + } + return list; + } + return list; + } + +} diff --git a/modules/sasha/src/test/java/com/hillel/elementary/javageeks/mycollection/MyListTest.java b/modules/sasha/src/test/java/com/hillel/elementary/javageeks/mycollection/MyListTest.java new file mode 100644 index 0000000..81a1948 --- /dev/null +++ b/modules/sasha/src/test/java/com/hillel/elementary/javageeks/mycollection/MyListTest.java @@ -0,0 +1,25 @@ +package com.hillel.elementary.javageeks.mycollection; + +import org.junit.jupiter.api.Test; + +import java.util.LinkedList; + +import static org.junit.jupiter.api.Assertions.*; + +class MyListTest { + @Test + void shouldsumMatrix(){ + int [][] matrixOne = {{0,2,0},{1,0,0},{0,0,3}}; + int [][] matrixTwo = {{0,2,0,1},{1,0,0,2},{0,0,3,0}}; + Integer[] num = {4,2,4}; + assertArrayEquals(num,MyList.actMatrix(matrixOne, matrixTwo,"sum").toArray()); + } + @Test + void shouldmultyplyMatrix(){ + int [][] matrixOne = {{0,2,0},{1,0,0},{0,0,3}}; + int [][] matrixTwo = {{0,2,0,1},{1,0,0,2},{0,0,3,0}}; + Integer[] num = {4, 1, 3}; + assertArrayEquals(num,MyList.actMatrix(matrixOne, matrixTwo,"multiply").toArray()); + } + +} \ No newline at end of file