Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Код из практики по д/з к 4му вебинару
- public class MatrixInteger implements Matrix {
- private int[][] matrix;
- public MatrixInteger() {
- }
- public MatrixInteger(final int[][] matrix) {
- this.matrix = matrix;
- }
- @Override
- public Matrix plus(final Matrix m) {
- int[][] result = null;
- if (m instanceof MatrixInteger) {
- result = new int[this.matrix.length][this.matrix.length];
- MatrixInteger mInt = (MatrixInteger) m;
- for (int i=0; i<this.matrix.length; i++) {
- for (int j=0; j<this.matrix[i].length; j++) {
- result[i][j] = this.matrix[i][j] + mInt.getMatrix()[i][j];
- }
- }
- }
- return new MatrixInteger(result);
- }
- Было сказано, что данный код почти готов для реализации в неизменном классе
- Моя реализация
- public final class Immutable extends MatrixImpl implements Matrix {
- private final int[][] matrix;
- public Immutable(final int[][] matrix) {
- this.matrix = matrix;
- }
- @Override
- public int[][] add(final MatrixImpl m) { //Проблема в том, что в методе стоит int[][] , а в примере объект.
- int[][] result = null;
- if(m instanceof Immutable) {
- result = new int[this.matrix.length][this.matrix.length];
- Immutable mInt = (Immutable) m;
- for (int i = 0; i < this.matrix.length; i++) {
- for (int j = 0; j < this.matrix[i].length; j++) {
- result[i][j] = this.matrix[i][j] + mInt.getMatrix()[i][j];
- }
- }
- }
- return new Immutable(result);
- }
- }
- Ругается на return new Immutable(result);, т.к. несовместимые типы данных - ожидается инт, а возвращается объект.
- Хотя если навести курсор на Imutable и (result) то оба интовые, в чем проблема не могу найти.
- поставить объект не могу, т.к. в обычном классе int[][]
- public int[][] add(int[][] m){
- int[][] result = new int [matrix.length][matrix.length];
- for (int i = 0; i<matrix.length; i++){
- for (int j = 0; j < matrix[i].length; j++) {
- result[i][j] = matrix[i][j] + m[i][j];
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement