### Question:
SparseMatrix extends Matrix { @Override @SuppressWarnings("squid:ForLoopCounterChangedCheck") protected final void forEach(final ElementVisitorFunction func) { final boolean[] copied = new boolean[1]; for (int i = 0; i < this.numRows; ++i) { final int iCopy = i; final double[] rowValues = this.values[i]; final int[] rowCols = this.cols[i]; int size = this.maxIndices[i]; for (int j = 0; j < size; ++j) { final int jCopy = j; copied[0] = false; func.visit( i, rowCols[j], rowValues[j], v -> { if (0.0 == v) { copied[0] = this.remove(iCopy, jCopy); } else { rowValues[jCopy] = v; } }); if (copied[0]) { size = this.maxIndices[i]; --j; } } } } SparseMatrix(final int numRows, final int numCols, final int initialCapacityPerRow); @Override final void forEach(final ReadOnlyElementVisitorFunction func); @Override MatrixNonZeroElementRowIterator getNonZeroElementRowIterator(final int row); int getNonZeroColumnCount(final int row); int getRowCapacity(final int row); @Override String toString(); int getNumEntries();  }### Answer:
@Test public void forEachReturnsAllNonZeroElements() { final Matrix matrix = this.createMatrix(3, 2, new double[] {2, 0, 0, 1, -5, 8}); final List<Double> values = new ArrayList<>(); matrix.forEach((row, col, value) -> values.add(value)); MatcherAssert.assertThat(values, IsEquivalent.equivalentTo(2.0, 1.0, -5.0, 8.0)); }