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.
import java.util.*;
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 row;
}
public int getCol() {
return col;
}
public int getValue() {
return value;
}
}
public class SparseArray {
private int numRows;
private int numCols;
private List<SparseArrayEntry> entries;
public SparseArray(int numRows, int numCols) {
this.numRows = numRows;
this.numCols = numCols;
this.entries = new ArrayList<>();
}
public void addEntry(int row, int col, int value) {
entries.add(new SparseArrayEntry(row, col, value));
}
public void removeColumn(int col) {
List<SparseArrayEntry> newEntries = new ArrayList<>();
for (SparseArrayEntry entry : entries) {
if (entry.getCol() != col) {
if (entry.getCol() > col) {
newEntries.add(new SparseArrayEntry(entry.getRow(), entry.getCol() - 1, entry.getValue()));
} else {
newEntries.add(entry);
}
}
}
entries = newEntries;
numCols--;
}
public void printArray() {
for (SparseArrayEntry entry : entries) {
System.out.println("Row: " + entry.getRow() + ", Col: " + entry.getCol() + ", Value: " + entry.getValue());
}
}
public static void main(String[] args) {
SparseArray sparseArray = new SparseArray(5, 5);
sparseArray.addEntry(0, 1, 1);
sparseArray.addEntry(0, 2, 2);
sparseArray.addEntry(0, 3, 3);
sparseArray.addEntry(1, 1, 4);
sparseArray.addEntry(1, 2, 5);
sparseArray.addEntry(1, 3, 6);
sparseArray.addEntry(2, 1, 7);
sparseArray.addEntry(2, 2, 8);
sparseArray.addEntry(2, 3, 9);
sparseArray.addEntry(3, 1, 10);
sparseArray.addEntry(3, 2, 11);
sparseArray.addEntry(3, 3, 12);
sparseArray.addEntry(4, 1, 13);
sparseArray.addEntry(4, 2, 14);
sparseArray.addEntry(4, 3, 15);
System.out.println("Before removing column:");
sparseArray.printArray();
sparseArray.removeColumn(1);
System.out.println("After removing column:");
sparseArray.printArray();
}
}
SparseArray.main(new String[0]);
Before removing column:
Row: 0, Col: 1, Value: 1
Row: 0, Col: 2, Value: 2
Row: 0, Col: 3, Value: 3
Row: 1, Col: 1, Value: 4
Row: 1, Col: 2, Value: 5
Row: 1, Col: 3, Value: 6
Row: 2, Col: 1, Value: 7
Row: 2, Col: 2, Value: 8
Row: 2, Col: 3, Value: 9
Row: 3, Col: 1, Value: 10
Row: 3, Col: 2, Value: 11
Row: 3, Col: 3, Value: 12
Row: 4, Col: 1, Value: 13
Row: 4, Col: 2, Value: 14
Row: 4, Col: 3, Value: 15
After removing column:
Row: 0, Col: 1, Value: 2
Row: 0, Col: 2, Value: 3
Row: 1, Col: 1, Value: 5
Row: 1, Col: 2, Value: 6
Row: 2, Col: 1, Value: 8
Row: 2, Col: 2, Value: 9
Row: 3, Col: 1, Value: 11
Row: 3, Col: 2, Value: 12
Row: 4, Col: 1, Value: 14
Row: 4, Col: 2, Value: 15