A two-dimensional array of integers in which most elements are zero is called as parse array.Because most elements have a value of zero, memory can be saved by storing only the non-zero values along with their row and column indexes. The following complete SparseArrayEntry class is used to represent non-zero elements in a sparse array. A SparseArrayEntry object cannot be modified after it has been constructed.

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.

b) Write the SparseArray method removeColumn. After removing a specified column from a sparse array:

  • 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. The sample object sparse from the beginning of the question is repeated for your convenience.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SparseArrayEntry {
    private int row;
    private int col;
    private int value;

    public SparseArrayEntry(int r, int c, int v) {
        this.row = r;
        this.col = c;
        this.value = v;
    }

    public int getRow() {
        return this.row;
    }

    public int getCol() {
        return this.col;
    }

    public void setCol(int col) {
        this.col = col;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getValue() {
        return this.value;
    }
}

public class SparseArray {
    private int numRows;
    private int numCols;
    private List<SparseArrayEntry> entries;

    public SparseArray(ArrayList<SparseArrayEntry> entries, int numRows, int numCols) {
        this.entries = entries;
        this.numRows = numRows;
        this.numCols = numCols;
    }

    public int getNumRows() {
        return this.numRows;
    }

    public int getNumCols() {
        return this.numCols;
    }

    public int getValueAt(int row, int col) {
        for (SparseArrayEntry s : this.entries) {
            if (s.getRow() == row && s.getCol() == col) {
                return s.getValue();
            }
        }
        return 0;
    }

    public void removeColumn(int col) {
        for (int i = 0; i < this.entries.size(); i++) {
            SparseArrayEntry s = this.entries.get(i);
            if (s.getCol() == col) {
                this.entries.remove(i);
                i--;  // Decrement index to account for the removed element
            } else if (s.getCol() > col) {
                s.setCol(s.getCol() - 1);
            }
        }
        this.numCols--;
    }

    public void displayArray() {
        for (int i = 0; i < this.numRows; i++) {
            for (int j = 0; j < this.numCols; j++) {
                System.out.print(getValueAt(i, j) + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        ArrayList<SparseArrayEntry> entries = new ArrayList<SparseArrayEntry>();

        entries.add(new SparseArrayEntry(1, 1, 5));
        entries.add(new SparseArrayEntry(2, 0, 1));
        entries.add(new SparseArrayEntry(3, 1, -9));
        entries.add(new SparseArrayEntry(1, 4, 4));

        SparseArray s = new SparseArray(entries, 4, 5);
        System.out.println(s.getValueAt(1, 1));

        System.out.println("First Array");
        s.displayArray();

        s.removeColumn(1);

        System.out.println("Second Array");
        s.displayArray();
    }
}

SparseArray.main(null);
5
First Array
0 0 0 0 0 
0 5 0 0 4 
1 0 0 0 0 
0 -9 0 0 0 
Second Array
0 0 0 0 
0 0 0 4 
1 0 0 0 
0 0 0 0