public RowMajorMatrixIterator nonZeroRowMajorIterator ( ) { return new RowMajorMatrixIterator ( rows , columns ) { private long limit = ( long ) rows * columns ; private long i = - 1 ; @ Override public int rowIndex ( ) { return ( int ) ( i / columns ) ; } @ Override public int columnIndex ( ) { return ( int ) ( i - ( ( i / columns ) * columns ) ) ; } @ Override public double get ( ) { return SparseMatrix . this . get ( rowIndex ( ) , columnIndex ( ) ) ; } @ Override public void set ( double value ) { SparseMatrix . this . set ( rowIndex ( ) , columnIndex ( ) , value ) ; } @ Override public boolean hasNext ( ) { while ( i + 1 < limit ) { i ++ ; if ( SparseMatrix . this . nonZeroAt ( rowIndex ( ) , columnIndex ( ) ) ) { i -- ; break ; } } return i + 1 < limit ; } @ Override public Double next ( ) { if ( ! hasNext ( ) ) { throw new NoSuchElementException ( ) ; } i ++ ; return get ( ) ; } } ; }
Returns a non - zero row - major matrix iterator .