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.
import java.util.ArrayList;
class SparseArrayEntry {
private int row;
private int col;
private int value;
public SparseArrayEntry(int row, int col, int value) {
this.row = row;
this.col = col;
this.value = value;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public int getValue() {
return value;
}
}
class SparseArray {
private ArrayList<SparseArrayEntry> entries;
public SparseArray() {
entries = new ArrayList<>();
}
public void addEntry(SparseArrayEntry entry) {
entries.add(entry);
}
// FRQ method part a, asking for getValueAt
public int getValueAt(int row, int col) {
for (SparseArrayEntry e : entries) {
if (e.getRow() == row && e.getCol() == col) {
return e.getValue();
}
}
return 0;
}
}
SparseArray sparse = new SparseArray();
// Adding some entries for testing
sparse.addEntry(new SparseArrayEntry(1, 1, 5));
sparse.addEntry(new SparseArrayEntry(2, 2, -3));
sparse.addEntry(new SparseArrayEntry(3, 1, -9));
// Testing the getValueAt method
System.out.println("(3, 1): " + sparse.getValueAt(3, 1)); // Output: -9
System.out.println("(3, 3): " + sparse.getValueAt(3, 3)); // Output: 0
(3, 1): -9
(3, 3): 0
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.
import java.util.ArrayList;
import java.util.Iterator;
class SparseArrayEntry {
private int row;
private int col;
private int value;
public SparseArrayEntry(int row, int col, int value) {
this.row = row;
this.col = col;
this.value = value;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public int getValue() {
return value;
}
}
class SparseArray {
private ArrayList<SparseArrayEntry> entries;
private int numRows;
private int numCols;
public SparseArray(int numRows, int numCols) {
entries = new ArrayList<>();
this.numRows = numRows;
this.numCols = numCols;
}
public void addEntry(SparseArrayEntry entry) {
entries.add(entry);
}
public void removeColumn(int col) {
Iterator<SparseArrayEntry> iterator = entries.iterator();
while (iterator.hasNext()) {
SparseArrayEntry entry = iterator.next();
if (entry.getCol() == col) {
iterator.remove();
} else if (entry.getCol() > col) {
entry = new SparseArrayEntry(entry.getRow(), entry.getCol() - 1, entry.getValue());
}
}
numCols--; // change the # of columns
}
public int getValueAt(int row, int col) {
for (SparseArrayEntry e : entries) {
if (e.getRow() == row && e.getCol() == col) {
return e.getValue();
}
}
return 0;
}
public int getNumRows() {
return numRows;
}
public int getNumCols() {
return numCols;
}
}
SparseArray sparse = new SparseArray(3, 3);
// testing the program
sparse.addEntry(new SparseArrayEntry(1, 1, 5));
sparse.addEntry(new SparseArrayEntry(1, 2, 7));
sparse.addEntry(new SparseArrayEntry(2, 2, -3));
sparse.addEntry(new SparseArrayEntry(3, 1, -9));
sparse.addEntry(new SparseArrayEntry(3, 3, 2));
// REMOVE COLUMN 2
sparse.removeColumn(2);
// Testing the getValueAt method
System.out.println("(1, 1): " + sparse.getValueAt(1, 1)); // Output: 5
System.out.println("(1, 2): " + sparse.getValueAt(1, 2)); // Output: 0 (removed)
System.out.println("(2, 2): " + sparse.getValueAt(2, 2)); // Output: 0 (also removed)
System.out.println("(3, 1): " + sparse.getValueAt(3, 1)); // Output: -9
System.out.println("(3, 3): " + sparse.getValueAt(3, 3)); // Output: 2
System.out.println("NumRows: " + sparse.getNumRows()); // Output: 3
System.out.println("NumCols: " + sparse.getNumCols()); // Output: 2
(1, 1): 5
(1, 2): 0
(2, 2): 0
(3, 1): -9
(3, 3): 2
NumRows: 3
NumCols: 2