I'm working on implementing a code where the output I'm looking for is the following
=== LINKED BAG 220 JAVA ========================================================== [+] Creating a CSC220 LinkedBag... [+] Adding.... these items to the bag: A _ _ G Bb A _ u n o A o d Bb A A l l [>] The bag now contains 18 string(s): l l A A Bb d o A o n u _ A Bb G _ _ A [+] Creating... a 2D test array with the below contents: A A A A A A B A Bb B Bb B C B _ A n u l l [+] Removing 2D test array items from the bag... [-] Converting 2D array to 1D... [-] Removing duplicates in 1D array... [>] The final 1D array now contains: A B Bb C _ n u l [-] Removing the final 1D array items from the bag... [>] The bag now contains 4 string(s): G o o d === LINKED BAG 220 JAVA ==========================================================
Here are the driver class (I CANNOT CHANGE ANYTHING HERE)
public class LinkedBagCSC220JavaDriver { public static void main(String[] args) { System.out.println("=== LINKED BAG 220 JAVA =========================================================="); System.out.println("[+] Creating a CSC220 LinkedBag..."); PrimaryDataStructureBagInterface<String> csc220Bag = new LinkedBag<>(); testAdd(csc220Bag); testRemoveAllOccurrences(csc220Bag); System.out.println("=== LINKED BAG 220 JAVA =========================================================="); } private static void displayBag(PrimaryDataStructureBagInterface<String> aBag) { System.out.print("[>] The bag now contains " + aBag.getCurrentSize() + " string(s): \t"); Object[] bagArray = aBag.toArray(); for (Object bagArray1 : bagArray) { System.out.print(bagArray1 + " "); } System.out.println(); } private static void testRemoveAllOccurrences(PrimaryDataStructureBagInterface<String> aBag) { // Removing all occurrences of the given entries from a bag System.out.println("[+] Creating... a 2D test array with the below contents: \t"); String[][] testArray = { { "A", "A", "A", "A", "A", "A" }, { "B", "A", "Bb", "B", "Bb", "B" }, { "C", "B", "_", "A" }, { "n", "u", "l", "l" } }; for (String[] row : testArray) { System.out.print("\t\t\t\t\t"); for (String col : row) { System.out.print(col + " "); } System.out.println(""); } aBag.removeAllOccurrences(testArray); // * testArray does not contain the bag with GOOD letters System.out.println(); // ! REMOVE later System.out.println(); // ! REMOVE later displayBag(aBag); } private static void testAdd(PrimaryDataStructureBagInterface<String> aBag) { // Adding strings String[] contentsOfBag = { "A", "_", "_", "G", "Bb", "A", "_", "u", "n", "o", "A", "o", "d", "Bb", "A", "A", "l", "l" // I'm looking for aBag variable with this content where I'll remove the common occurrence with newOneDarray from my removeAllOccurence method }; System.out.print("[+] Adding.... these items to the bag: \t"); for (String entry : contentsOfBag) { aBag.add(entry); System.out.print(entry + " "); } System.out.println(); displayBag(aBag); } }
Here is the removeAllOcurrence method I'm working on (this is just the method which is a part of another class that I can change)
public boolean removeAllOccurrences(T[][] entries) { System.out.println("[+] Removing 2D test array items from the bag..."); System.out.println("[-] Converting 2D array to 1D..."); // convert 2D array into 1D ArrayList List<T> oneDArray = new ArrayList<>(); for (int row = 0; row < entries.length; row++) { for (int col = 0; col < entries[row].length; col++) oneDArray.add(entries[row][col]); // System.out.println(oneDArray); } // System.out.println("1D Array : " + oneDArray); System.out.println("[-] Removing duplicates in 1D array..."); // using stream to remove all duplicate from 1D ArrayList List<T> newOneDArray = oneDArray.stream() .distinct() .collect(Collectors.toList()); // System.out.println("1D Array after removing duplicate into new list : " + // newOneDArray); System.out.print("[>] The final 1D array now contains: "); for (int i = 0; i < newOneDArray.size(); i++) { System.out.print(newOneDArray.get(i) + " "); // print every element of newOneDArray (ArrayList) } System.out.println(); System.out.println("[-] Removing the final 1D array items from the bag..."); System.out.println(); System.out.println(); System.out.println(); System.out.println("//DEBUGGING// array entries : " + Arrays.deepToString(entries)); System.out.println("//DEBUGGING// oneDArray : " + oneDArray); System.out.println("//DEBUGGING// newDArray without duplicates : " + newOneDArray.toString()); return true; }
Here is my output
=== LINKED BAG 220 JAVA ========================================================== [+] Creating a CSC220 LinkedBag... [+] Adding.... these items to the bag: A _ _ G Bb A _ u n o A o d Bb A A l l [>] The bag now contains 18 string(s): l l A A Bb d o A o n u _ A Bb G _ _ A [+] Creating... a 2D test array with the below contents: A A A A A A B A Bb B Bb B C B _ A n u l l [+] Removing 2D test array items from the bag... [-] Converting 2D array to 1D... [-] Removing duplicates in 1D array... [>] The final 1D array now contains: A B Bb C _ n u l [-] Removing the final 1D array items from the bag... //As you can see none of those variable return me the variable with the content l l A A Bb d o A o n u _ A Bb G _ _ A //DEBUGGING// array entries : [[A, A, A, A, A, A], [B, A, Bb, B, Bb, B], [C, B, _, A], [n, u, l, l]] //DEBUGGING// oneDArray : [A, A, A, A, A, A, B, A, Bb, B, Bb, B, C, B, _, A, n, u, l, l] //DEBUGGING// newDArray without duplicates : [A, B, Bb, C, _, n, u, l] [>] The bag now contains 18 string(s): l l A A Bb d o A o n u _ A Bb G _ _ A === LINKED BAG 220 JAVA ==========================================================
I'm on the part of removing all Occurrence from newOneDarray to the aBag from the driver class. I was thinking of iterating going through each newOneDArray and aBag and removing the common occurrence inside aBag. The issue is I do not know how to get access to aBag inside that method. removeAllOcurrence. Is there a way? (Knowing that I cannot modify the driver class). Or do you have any suggestions on how to achieve what I'm looking for?