(a) Write the SparseArray method getValueAt. The method returns the value of the sparse array element at a given row and column in the sparse array. If the list entries contains an entry with the specified row and column, the value associated with the entry is returned. If there is no entry in entries corresponding to the specified row and column, 0 is returned. In the example above, the call sparse.getValueAt(3, 1) would return -9, and sparse.getValueAt(3, 3) would return 0.

public class SparseArrayEntry {
    /** The row index and column index for this entry in the sparse array */
    private int row;
    private int col;
    
    /** The value of this entry in the sparse array */
    private int value;
    
    /** Constructs a SparseArrayEntry object that represents a sparse array element
    * with row index r and column index c, containing value v.
    */
    public SparseArrayEntry(int r, int c, int v) {
        row = r;
        col = c;
        value = v;
    }
    
    /** Returns the row index of this sparse array element. */
    public int getRow() { 
        return row; 
    }

    /** Returns the column index of this sparse array element. */
    public int getCol() { 
        return col; 
    }
    
    /** Returns the value of this sparse array element. */
    public int getValue() { 
        return value; 
    }
}

public class SparseArray {
    /** The list of entries representing the non-zero elements of the sparse array. Entries are stored in the
    * list in no particular order. Each non-zero element is represented by exactly one entry in the list.
    */
    private List<SparseArrayEntry> entries;
    
    /** Constructs an empty SparseArray. */
    public SparseArray() { 
        entries = new ArrayList<SparseArrayEntry>(); 
    }

    /** Adds an Entry to the Sparse Array */
    public void addEntry(SparseArrayEntry entry) {
        entries.add(entry);
    }
    
    /** Returns the value of the element at row index row and column index col in the sparse array.
    * Precondition: 0  row < getNumRows()
    * 0  col < getNumCols()
    */
    public int getValueAt(int row, int col) { /* to be implemented in part (a) */ 
        for (int i = 0; i < entries.size(); i++) {
            SparseArrayEntry entry = entries.get(i);
            int rowNumber = entry.getRow();
            int colNumber = entry.getCol();

            if (rowNumber == row && colNumber == col) {
                return entry.getValue();
            }
        }
        return 0;
    }

    public static void main(String args[]) {
        SparseArray sparseArray = new SparseArray();
    
        // Add entries to the sparse array
        sparseArray.addEntry(new SparseArrayEntry(1, 4, 4));
        sparseArray.addEntry(new SparseArrayEntry(2, 0, 1));
        sparseArray.addEntry(new SparseArrayEntry(3, 1, -9));
        sparseArray.addEntry(new SparseArrayEntry(1, 1, 5));

        System.out.println("Value at (3, 1): " + sparseArray.getValueAt(3, 1));
        System.out.println("Value at (3, 3): " + sparseArray.getValueAt(3, 3));
    }
}

SparseArray.main(null);
Value at (3, 1): -9
Value at (3, 3): 0

PART B: Write the SparseArray method removeColumn. After removing a specified column from a sparsearray:

  • All entries in the list entries with column indexes matching col are removed from the list.
  • All entries in the list entries with column indexes greater than col are replaced by entries with column indexes that are decremented by one (moved one column to the left).
  • The number of columns in the sparse array is adjusted to reflect the column removed.
public class SparseArrayEntry {
    /** The row index and column index for this entry in the sparse array */
    private int row;
    private int col;
    
    /** The value of this entry in the sparse array */
    private int value;
    
    /** Constructs a SparseArrayEntry object that represents a sparse array element
    * with row index r and column index c, containing value v.
    */
    public SparseArrayEntry(int r, int c, int v) {
        row = r;
        col = c;
        value = v;
    }
    
    /** Returns the row index of this sparse array element. */
    public int getRow() { 
        return row; 
    }

    public void setRow(int newRow) {
        this.row = newRow;
    }
    
    /** Returns the column index of this sparse array element. */
    public int getCol() { 
        return col; 
    }

    public void setCol(int newCol) {
        this.col = newCol;
    }
    
    /** Returns the value of this sparse array element. */
    public int getValue() { 
        return value; 
    }

    public void setValue(int newValue) {
        this.value = newValue;
    }

    //Allows for the printing out of a SparseArrayEntry to the console in a visually appealing fashion
    @Override
    public String toString() {
        return "SparseArrayEntry{" +
            "row=" + row +
            ", col=" + col +
            ", value=" + value +
            '}';
    }
}

public class SparseArray {
    /** The number of rows and columns in the sparse array. */
    private int numRows;
    private int numCols;
    /** The list of entries representing the non-zero elements of the sparse array. Entries are stored in the
    * list in no particular order. Each non-zero element is represented by exactly one entry in the list.
    */
    private List<SparseArrayEntry> entries;
    
    /** Constructs an empty SparseArray. */
    public SparseArray() { 
        entries = new ArrayList<SparseArrayEntry>(); 
    }

    /** Adds an Entry to the Sparse Array */
    public void addEntry(SparseArrayEntry entry) {
        entries.add(entry);
    }

    /** A nice way to print out the entries so we can ensure the remove column method works */
    public void printEntries() {
        for (SparseArrayEntry entry : entries) {
            System.out.println(entry);
        }
    }    


    /** Returns the number of rows in the sparse array. */
    public int getNumRows() { 
        return numRows; 
    }

    public void setNumRows(int newNumRows) {
        this.numRows = newNumRows;
    }

    /** Returns the number of columns in the sparse array. */
    public int getNumCols() { 
        return numCols; 
    }

    public void setNumCols(int newNumCols) {
        this.numCols = newNumCols;
    }
    
    /** Returns the value of the element at row index row and column index col in the sparse array.
    * Precondition: 0  row < getNumRows()
    * 0  col < getNumCols()
    */
    public int getValueAt(int row, int col) { /* to be implemented in part (a) */ 
        for (int i = 0; i < entries.size(); i++) {
            SparseArrayEntry entry = entries.get(i);
            int rowNumber = entry.getRow();
            int colNumber = entry.getCol();

            if (rowNumber == row && colNumber == col) {
                return entry.getValue();
            }
        }
        return 0;
    }
    
    /** Removes the column col from the sparse array.
    * Precondition: 0  col < getNumCols()
    */
    public void removeColumn(int col) {
        Iterator<SparseArrayEntry> iterator = entries.iterator();
        while (iterator.hasNext()) {
            SparseArrayEntry entry = iterator.next();
            int colNumber = entry.getCol();
    
            if (colNumber == col) {
                iterator.remove();
            } else if (colNumber > col) {
                entry.setCol(colNumber - 1);
            }
        }
        this.setNumCols(this.getNumCols() - 1);
    }    
    // There may be instance variables, constructors, and methods that are not shown.

    public static void main(String args[]) {
        SparseArray sparseArray = new SparseArray();

        // Set the number of rows and columns
        sparseArray.setNumRows(6);
        sparseArray.setNumCols(5);
    
        // Add entries to the sparse array
        sparseArray.addEntry(new SparseArrayEntry(1, 4, 4));
        sparseArray.addEntry(new SparseArrayEntry(2, 0, 1));
        sparseArray.addEntry(new SparseArrayEntry(3, 1, -9));
        sparseArray.addEntry(new SparseArrayEntry(1, 1, 5));
        
        System.out.println("\nEntries Before removing column 1:");
        sparseArray.printEntries();
    
        sparseArray.removeColumn(1);

        System.out.println("numRows: " + sparseArray.getNumRows());
        System.out.println("numCols: " + sparseArray.getNumCols());

        System.out.println("\nEntries After removing column 1:");
        sparseArray.printEntries();
    }
}

SparseArray.main(null);
Entries Before removing column 1:
SparseArrayEntry{row=1, col=4, value=4}
SparseArrayEntry{row=2, col=0, value=1}
SparseArrayEntry{row=3, col=1, value=-9}
SparseArrayEntry{row=1, col=1, value=5}
numRows: 6
numCols: 4

Entries After removing column 1:
SparseArrayEntry{row=1, col=3, value=4}
SparseArrayEntry{row=2, col=0, value=1}

In this 2D Arrays FRQ, I tackled the challenge of efficiently representing and manipulating a sparse array, a two-dimensional array primarily composed of zeros. The SparseArray class, along with the SparseArrayEntry class, provided the foundation for this task.

Part A required me to implement the getValueAt method within the SparseArray class. This method was essential for retrieving the value of an array element at a specified row and column. The key challenge here was to search through the list of non-zero entries efficiently. I iterated through the list, checking each entry’s row and column against the provided parameters. If a match was found, the corresponding value was returned; otherwise, the method defaulted to returning zero.

In Part B, I developed the removeColumn method, a more complex task involving the modification of the array’s structure. The method had to iterate through the array entries, removing those in the specified column and adjusting the column indices of the remaining entries. This part of the FRQ tested my ability to not only access and evaluate data within a collection but also to modify the collection itself. In developing this method, I initially faced challenges with using a for loop for list modification, as removing elements shifted the indices of subsequent elements, leading to errors. To solve this, I learned about and implemented an iterator, which allowed for safe removal and adjustment of elements without affecting the traversal,

Currently, our project does not implement any 2D Array data objects. However, we expect this to soon be remedied with the introduction of the neural network for moneyline NBA game predictions. The incorporation of neural networks will naturally lead us into the realm of 2D arrays, as they are essential in representing the complex, multi-dimensional data structures typical in machine learning models. 2D arrays, in this context, will be pivotal for handling weights, biases, and activations within the network layers.