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 List<SparseArrayEntry> entries;
private int numRows;
private int numCols;
public SparseArray(int numRows, int numCols) {
this.numRows = numRows;
this.numCols = numCols;
entries = new ArrayList<>();
}
public void addEntry(int row, int col, int value) {
entries.add(new SparseArrayEntry(row, col, value));
}
public int getNumRows() {
return numRows;
}
public int getNumCols() {
return numCols;
}
public int getValueAt(int row, int col) {
for (SparseArrayEntry e : entries) {
if (e.getRow() == row && e.getCol() == col) {
return e.getValue();
}
}
return 0;
}
// Test for printing array
public void printSparseArray() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
int value = getValueAt(i, j);
System.out.print(value + " ");
}
System.out.println(); // Move to the next line after each row
}
}
// Test
public static void main(String[] args) {
SparseArray sparseArray = new SparseArray(5, 5);
sparseArray.addEntry(1, 4, 4);
sparseArray.addEntry(2, 0, 1);
sparseArray.addEntry(3, 1, -9);
sparseArray.addEntry(1, 1, 5);
sparseArray.printSparseArray();
System.out.println();
//Test with Row 2 Column 5
System.out.println(sparseArray.getValueAt(1, 4));
//Test with Row 3 Column 1
System.out.println(sparseArray.getValueAt(2, 0));
//Test with Row 5 Column 5
System.out.println(sparseArray.getValueAt(4, 4));
}
}
SparseArray.main(null);
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
4
1
0
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 List<SparseArrayEntry> entries;
private int numRows;
private int numCols;
public SparseArray(int numRows, int numCols) {
this.numRows = numRows;
this.numCols = numCols;
entries = new ArrayList<>();
}
public void addEntry(int row, int col, int value) {
entries.add(new SparseArrayEntry(row, col, value));
}
public int getNumRows() {
return numRows;
}
public int getNumCols() {
return numCols;
}
public int getValueAt(int row, int col) {
for (SparseArrayEntry e : entries) {
if (e.getRow() == row && e.getCol() == col) {
return e.getValue();
}
}
return 0;
}
// Test for printing array
public void printSparseArray() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
int value = getValueAt(i, j);
System.out.print(value + " ");
}
System.out.println(); // Move to the next line after each row
}
}
public void removeColumn(int col) {
int i = 0;
while (i < entries.size()){
SparseArrayEntry e = entries.get(i);
if (e.getCol() == col){
entries.remove(i);
}
else if (e.getCol() > col){
entries.set(i, new SparseArrayEntry(e.getRow(), e.getCol()-1, e.getValue()));
i++;
}
else{
i++;
}
}
numCols--;
}
public static void main(String[] args) {
SparseArray sparseArray = new SparseArray(5, 5);
sparseArray.addEntry(1, 4, 4);
sparseArray.addEntry(2, 0, 1);
sparseArray.addEntry(3, 1, -9);
sparseArray.addEntry(1, 1, 5);
sparseArray.printSparseArray();
System.out.print("Value at Row 2 Column 5: ");
System.out.println(sparseArray.getValueAt(1, 4));
System.out.print("Value at Row 3 Column 1: ");
System.out.println(sparseArray.getValueAt(2, 0));
System.out.print("Value at Row 5 Column 5: ");
System.out.println(sparseArray.getValueAt(4, 4));
System.out.println();
sparseArray.removeColumn(1);
sparseArray.printSparseArray();
System.out.print("Value at Row 2 Column 5: ");
System.out.println(sparseArray.getValueAt(1, 4));
System.out.print("Value at Row 3 Column 1: ");
System.out.println(sparseArray.getValueAt(2, 0));
System.out.print("Value at Row 5 Column 5: ");
System.out.println(sparseArray.getValueAt(4, 4));
}
}
SparseArray.main(null);
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
Value at Row 2 Column 5: 4
Value at Row 3 Column 1: 1
Value at Row 5 Column 5: 0
0 0 0 0
0 0 0 4
1 0 0 0
0 0 0 0
0 0 0 0
Value at Row 2 Column 5: 0
Value at Row 3 Column 1: 1
Value at Row 5 Column 5: 0