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 ro 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.
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 int getValueAt(int row, int col) {
for (SparseArray i:entries ){
if (i.getrow== row && i.getcol== col){
return i.getValue();
}
else {
return 0;
}
}
}
| for (SparseArray i:entries ){
cannot find symbol
symbol: variable entries
| for (SparseArray i:entries ){
cannot find symbol
symbol: class SparseArray
| public int getValueAt(int row, int col) {
| for (SparseArray i:entries ){
| if (i.getrow== row && i.getcol== col){
| return i.getValue();
| }
| else {
| return 0;
| }
| }
| }
missing return statement
Can not show this part running, because this requires the methods from the provided class (which are written by CB in comments, not real java. Let’s assume this is correct, I will get more feedback when I peer grade)
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.
Repeated Class information for mine, and peer grader’s convenience :
public void removeColumn(int col){
int x = 0;
while (x < entries.size()){
if (x.getCol() == col){
entries.remove(x);
}
else if (i.getCol() > col) {
entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));
i ++;
}
else {
i ++;
}
numCols = numCols - 1;
}
}
| while (x < entries.size()){
cannot find symbol
symbol: variable entries
| if (x.getCol() == col){
int cannot be dereferenced
| entries.remove(x);
cannot find symbol
symbol: variable entries
| else if (i.getCol() > col) {
cannot find symbol
symbol: variable i
| entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));
cannot find symbol
symbol: variable i
| entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));
cannot find symbol
symbol: class SparseArrayEntry
| entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));
cannot find symbol
symbol: variable i
| entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));
cannot find symbol
symbol: variable i
| entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));
cannot find symbol
symbol: variable i
| entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));
cannot find symbol
symbol: variable entries
| i ++;
cannot find symbol
symbol: variable i
| i ++;
cannot find symbol
symbol: variable i
| numCols = numCols - 1;
cannot find symbol
symbol: variable numCols
| numCols = numCols - 1;
cannot find symbol
symbol: variable numCols
I was a little confused on the second bullet, the else if . Are we replacing the new col with the contents of the one before it? Are we deleting the one before it so is the new col just replacing the old one? Or will the duplicates exist?