Tenho uma classe Planilha que possui uma Matriz da classe Celula.
A classe Planilha tem um método SetCel(valor, linha, coluna), que insere um valor para a Matriz de Celulas nos índices passados por parâmetros.
Tenho outra Classe Chamada Formula que tem um método Soma(linha,coluna, linha2,coluna2) que recebe como parâmetros quatro inteiros que seriam índices da Matriz de Celula que fica na classe Planilha. Preciso acessar a Matriz da classe Planilha nesse método da Classe Formula, para então através dos índices planilha[0][0] somar com planilha[1][1].
/**
* @author itamar
*/
public class SpreadSheet {
private Map<Cell, Integer> cells; // Cell/Value
private SpreadSheet() {
this.cells = new HashMap<>();
}
public int getCellValue(Cell cell) {
return cells.getOrDefault(cell, 0);
}
public Stream<Integer> getValueStream(CellSelection selection) {
return selection.getCellStream().map(e -> getCellValue(e));
}
public void setCellValue(Cell cell, int value) {
cells.computeIfAbsent(cell, e -> value);
}
public void setCellValues(CellSelection selection, Integer value) {
selection.getCellStream().forEach(e -> setCellValue(e, value));
}
static class CellSelection {
private final Set<Cell> selectedCells;
public CellSelection(Cell... initialSelection) {
this.selectedCells = new HashSet<>();
Stream.of(initialSelection).forEach(e -> this.addCell(e));
}
public CellSelection addCell(Cell cell) {
this.selectedCells.add(cell);
return this;
}
public CellSelection removeCell(Cell cell) {
this.selectedCells.remove(cell);
return this;
}
public OpenSelection from(Cell cell) {
return new OpenSelection(cell);
}
class OpenSelection {
private final Cell fromCell;
public OpenSelection(Cell fromCell) {
this.fromCell = fromCell;
}
public CellSelection to(Cell toCell) {
for (int col = fromCell.getCol(); col <= toCell.getCol(); col++) {
for (int row = fromCell.getRow(); row <= toCell.getRow(); row++) {
addCell(new Cell(col, row));
}
}
return CellSelection.this;
}
}
public Stream<Cell> getCellStream() {
return selectedCells.stream();
}
}
static class Cell {
private final int col;
private final int row;
public Cell(int col, int row) {
this.col = col;
this.row = row;
}
public int getCol() {
return col;
}
public int getRow() {
return row;
}
public CellSelection toCellSelection() {
return new CellSelection(this);
}
@Override
public boolean equals(Object o) {
// Intentionally only col and row
if (this == o) return true;
if (!(o instanceof Cell)) return false;
Cell cell = (Cell) o;
if (col != cell.col) return false;
return row == cell.row;
}
@Override
public int hashCode() {
// Only col and row intentionally
int result = col;
result = 31 * result + row;
return result;
}
}
public static void main(String... args) {
SpreadSheet spreadSheet = new SpreadSheet();
spreadSheet.setCellValue(new Cell(1, 1), 10); // Set value for 1 X 1
CellSelection setSelection = // Let's try with many cells
new CellSelection(new Cell(2, 2))
.addCell(new Cell(2, 3))
.from(new Cell(3, 3)) // Select range from 3 X 3 to 5 X 10
.to(new Cell(5, 10));
spreadSheet.setCellValues(setSelection, 4);
// Let's select a few cells for the operations
CellSelection opSelection = new CellSelection().from(new Cell(1, 1)).to(new Cell(5, 5));
// Sum
System.out.println("Sum: " +
spreadSheet.getValueStream(opSelection)
.mapToInt(e -> e) // This will be useful when you have other data types to handle
.sum()
);
// Count
System.out.println("Count: " +
spreadSheet.getValueStream(opSelection)
.mapToInt(e -> e) // This will be useful when you have other data types to handle
.count()
);
// Average
System.out.println("Average: " +
spreadSheet.getValueStream(opSelection)
.mapToInt(e -> e) // This will be useful when you have other data types to handle
.average().getAsDouble()
);
}
}
Você pode passar a matriz que você tem como parâmetro, ex: Soma(linha,coluna, linha2,coluna2,int[][[] matriz)
depois no método só fazer:
return matriz[linha][coluna]+matriz[linha2][coluna2];