Advertisement
Atem85

w4

Sep 20th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. Код из практики по д/з к 4му вебинару
  2. public class MatrixInteger implements Matrix {
  3.  
  4.     private int[][] matrix;
  5.  
  6.     public MatrixInteger() {
  7.     }
  8.  
  9.     public MatrixInteger(final int[][] matrix) {
  10.         this.matrix = matrix;
  11.     }
  12.  
  13.     @Override
  14.     public Matrix plus(final Matrix m) {
  15.         int[][] result = null;
  16.         if (m instanceof MatrixInteger) {
  17.             result = new int[this.matrix.length][this.matrix.length];
  18.             MatrixInteger mInt = (MatrixInteger) m;
  19.             for (int i=0; i<this.matrix.length; i++) {
  20.                 for (int j=0; j<this.matrix[i].length; j++) {
  21.                     result[i][j] = this.matrix[i][j] + mInt.getMatrix()[i][j];
  22.                 }
  23.             }
  24.         }
  25.         return new MatrixInteger(result);
  26.     }
  27.  
  28. Было сказано, что данный код почти готов для реализации в неизменном классе
  29.  
  30. Моя реализация
  31.  
  32. public final class Immutable extends MatrixImpl implements Matrix {
  33.    private final int[][] matrix;
  34.  
  35.  
  36.  
  37.     public Immutable(final int[][] matrix) {
  38.         this.matrix = matrix;
  39.     }
  40.  
  41.     @Override
  42.     public int[][] add(final MatrixImpl m) { //Проблема в том, что в методе стоит int[][] , а в примере объект.
  43.         int[][] result = null;
  44.         if(m instanceof Immutable) {
  45.             result = new int[this.matrix.length][this.matrix.length];
  46.             Immutable mInt = (Immutable) m;
  47.             for (int i = 0; i < this.matrix.length; i++) {
  48.                 for (int j = 0; j < this.matrix[i].length; j++) {
  49.                     result[i][j] = this.matrix[i][j] + mInt.getMatrix()[i][j];
  50.                 }
  51.             }
  52.         }
  53.         return new Immutable(result);
  54.     }
  55. }
  56.  
  57. Ругается на   return new Immutable(result);, т.к. несовместимые типы данных - ожидается инт, а возвращается объект.
  58. Хотя если навести курсор на Imutable  и (result) то оба интовые, в чем проблема не могу найти.
  59.  
  60. поставить объект не могу, т.к. в обычном классе int[][]
  61.     public int[][] add(int[][] m){
  62.         int[][] result = new int [matrix.length][matrix.length];
  63.         for (int i = 0; i<matrix.length; i++){
  64.             for (int j = 0; j < matrix[i].length; j++) {
  65.                 result[i][j] = matrix[i][j] + m[i][j];
  66.             }
  67.         }
  68.         return result;
  69.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement