Question 3
A two-dimensional array of integers in which most elements are zero is called a sparse 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.
The SparseArray class represents a sparse array. It contains a list of SparseArrayEntry objects, each of which represents one of the non-zero elements in the array. The entries representing the non-zero elements are stored in the list in no particular order. Each non-zero element is represented by exactly one entry in the list.
The following table shows an example of a two-dimensional sparse array. Empty cells in the table indicate zero values.
The sample array can be represented by a SparseArray object, sparse, with the following instance variable values. The items in entries are in no particular order; one possible ordering is shown below.
(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.
Complete method getValueAt below.
public class SparseArrayEntry {
private int row;
private int col;
private int value;
// initializing each
public SparseArrayEntry(int r, int c, int v) {
row = r;
col = c;
value = v;
}
// getter, return row
public int getRow() {
return row;
}
// getter, return collumn
public int getCol() {
return col;
}
// getter, return value
public int getValue() {
return value;
}
}
import java.util.ArrayList;
import java.util.List;
public class SparseArray {
private int numRows; // number of rows
private int numCols; // number of column
private List<SparseArrayEntry> entries;
// constructor, initialize the spares array with examples
public SparseArray() {
entries = new ArrayList<SparseArrayEntry>();
// example from above
entries.add(new SparseArrayEntry(1, 4, 4));
entries.add(new SparseArrayEntry(2, 0, 1));
entries.add(new SparseArrayEntry(3, 1, -9));
entries.add(new SparseArrayEntry(1, 1, 5));
// number of rows
numRows = 6;
// number of collumns
numCols = 5;
}
// getter, return numer of rows
public int getNumRows() {
return numRows;
}
// getter, return number of collumns
public int getNumCols() {
return numCols;
}
// return value in specific row
public int getValueAt(int row, int col) {
for (SparseArrayEntry entry : entries) {
if (entry.getRow() == row && entry.getCol() == col)
// return if value is found
return entry.getValue();
}
// return 0 if nothing found
return 0;
}
// testing
public static void main(String[] args) {
SparseArray sparse = new SparseArray();
System.out.println("(3, 1): " + sparse.getValueAt(3, 1)); // returns -9
System.out.println("(3, 3): " + sparse.getValueAt(3, 3)); // returns 0, nothing should be found
}
}
SparseArray.main(null);
(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.
The sample object sparse from the beginning of the question is repeated for your convenience.
The shaded entries in entries, below, correspond to the shaded column above.
When sparse has the state shown above, the call sparse.removeColumn(1) could result insparse having the following values in its instance variables (since entries is in no particular order, itwould be equally valid to reverse the order of its two items). The shaded areas below show the changes.
public class SparseArray1 {
private int numRows;
private int numCols;
private List<SparseArrayEntry> entries;
public SparseArray1() { // Corrected constructor name
entries = new ArrayList<SparseArrayEntry>();
entries.add(new SparseArrayEntry(1, 4, 4));
entries.add(new SparseArrayEntry(2, 0, 1));
entries.add(new SparseArrayEntry(3, 1, -9));
entries.add(new SparseArrayEntry(1, 1, 5));
numRows = 6;
numCols = 5;
}
public int getNumRows() {
return numRows;
}
public int getNumCols() {
return numCols;
}
public int getValueAt(int row, int col) {
for (SparseArrayEntry entry : entries) {
if (entry.getRow() == row && entry.getCol() == col)
return entry.getValue();
}
return 0; // If no entry is found
}
// everything is similar to above until here
// removing the column from array
public void removeColumn(int col) {
for (int i = entries.size() - 1; i >= 0; i--) {
SparseArrayEntry entry = entries.get(i);
if (entry.getCol() == col) {
// removing an entry if it belongs to specific column
entries.remove(i);
} else if (entry.getCol() > col) {
// if entry collumn is greater than specific column decrease index by 1
entries.set(i, new SparseArrayEntry(entry.getRow(), entry.getCol() - 1, entry.getValue()));
}
}
// decreasing index by 1
numCols--;
}
// displaying the array
public void display() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
System.out.print(getValueAt(i, j) + " ");
}
System.out.println();
}
}
// testing the class
public static void main(String[] args) {
// creating the sparsearray1 object
SparseArray1 sparse = new SparseArray1();
System.out.println("original :");
// displaying original array
sparse.display();
// removing 1 collumn
sparse.removeColumn(1);
System.out.println("removing 1 collumn :");
// display after removing 1 column
sparse.display();
}
}
SparseArray1.main(null);
original :
0 0 0 0 0
0 5 0 0 4
1 0 0 0 0
0 -9 0 0 0
0 0 0 0 0
0 0 0 0 0
removing 1 collumn :
0 0 0 0
0 0 0 4
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0