【问题标题】:New to Multidimensional Arrays多维数组的新手
【发布时间】:2013-02-11 10:38:34
【问题描述】:

我想构建一个程序来操作多维数组,这是我想学习的一个新概念......

我认为它可能有几种方法...

public int get(int row, int column)
//  a method to get the value in row/column
//  keep in mind columns/rows start at 0


public void set(int row, int column, int value)
// a method to set the matrix element to the value
// again keep in mind columns/rows start at 0


public void negate()
// method that negates through each element

public void add(Matrix m)
// adds the matrix m to this

谁能告诉我这是否是正确的方法,我该怎么做? 提前致谢

【问题讨论】:

  • 听起来不错。您是在询问要使用哪种数据结构,或者您将如何实现这些方法?
  • 看起来不错,并且您的代码有描述每种方法的注释
  • @chm052 实现它们! :D
  • 方法命名取反。这是什么意思?方法名称应该定义方法的功能
  • 尝试自己编写程序。如果您遇到问题,请提出问题。

标签: java multidimensional-array matrix


【解决方案1】:

您的界面(即您计划的方法)似乎非常合理。

至于一般的多维数组操作,有很多可用的教程,例如hereherehereherehere

为了帮助您入门,简单的代码示例:

创建很棒的多维数组

int[][] multi = new int[2][3]; // this mda has two rows and three columns

访问他们的 rad 元素

int el = multi[0][1]; // el is in the first row, second column

像老板一样遍历它们

for (int rowNum = 0; rowNum < multi.length; rowNum++) {
    for (int colNum = 0; colNum < multi[rowNum].length; colNum++) {
        int currentElement = multi[rowNum][colNum];
    }
}

【讨论】:

    【解决方案2】:

    您的方法看起来总体上是明智的。

    需要考虑的一些事情:

    • int 对于数学矩阵的基本数据类型来说并不是一个好的选择。例如,您通常无法获得int 矩阵的精确逆矩阵。您确定不想改用 double 吗?
    • 使此类向量/矩阵不可变是个好主意。在这种情况下,addsetnegate 应该返回一个新矩阵。支持可变性的唯一真正论据是性能(但在这种情况下……您应该使用现有的库而不是滚动自己的库!)

    如果你想使用或研究一个现有的库来做这样的事情(以及更多),那么你可能会对我相当广泛的向量/矩阵数学库感兴趣:

    Vectorz 专门针对原始 double 数组进行快速数值运算,但同样的原则也适用于任何类型的数组/矩阵。

    【讨论】:

      【解决方案3】:

      谁能告诉我这是否是正确的方法,我该怎么做?

      是的,绝对的。

      但是,按照惯例,cmets 应该在方法声明的上方。并调用方法头。

      例如:

      //  a method to get the value in row/column
      //  keep in mind columns/rows start at 0
      public int get(int row, int column)
      

      【讨论】:

        【解决方案4】:

        看看这个实现并分析它,如果这是为了家庭作业,那么如果你不知道发生了什么,那么“剪贴板继承”对你没有好处。

            public class Matrix
        {
            private double[][] matrixData;
        
            private int col;
        
            private int row;
        
            /**
             * Create matrix of zero size
             */
            public Matrix()
            {
                this(0);
            }
        
            public Matrix(double[][] matrixData)
            {
                this.matrixData = matrixData;
        
                this.row = matrixData.length;
                this.col = matrixData[0].length;
            }
        
            public Matrix(double[] matrixData)
            {
                this.row = matrixData.length;
                for (int i = 0, size = matrixData.length; i < size; i++)
                {
                    this.matrixData[0][i] = matrixData[i];
                }
            }
        
            public Matrix(int size)
            {
                this.matrixData = new double[size][size];
                this.col = size;
                this.row = size;
            }
        
            public Matrix(int row, int col)
            {
                matrixData = new double[row][col];
                this.row = row;
                this.col = col;
            }
        
            /**
             * Static method which creates a matrix with a single column.
             * 
             * @param input
             * @return
             */
            public static Matrix createColumnMatrix(final double input[])
            {
                double result[][] = new double[input.length][1];
                for (int i = 0; i < input.length; i++)
                    result[i][0] = input[i];
        
                return new Matrix(result);
            }
        
            /**
             * Static method which creates a matrix with a single row.
             * 
             * @param input
             * @return
             */
            public static Matrix createRowMatrix(final double input[])
            {
                double result[][] = new double[1][input.length];
                for (int i = 0, size = input.length; i < size; i++)
                    result[0][i] = input[i];
        
                return new Matrix(result);
            }
        
            /**
             * Convert the matrix into a one dimentional matrix
             * 
             * @return
             */
            public double[] toPackedArray()
            {
                int size = row * col;
                int index = 0;
                double[] results = new double[size];
                for (int i = 0; i < row; i++)
                    for (int j = 0; j < col; j++)
                        results[index++] = matrixData[i][j];
        
                return results;
            }
        
            /**
             * Adds the specified value to given cell in the matrix.
             * 
             * @param row
             * @param col
             * @param value
             */
            public void add(final int row, final int col, final double value)
            {
                matrixData[row][col] += value;
            }
        
            /**
             * Sets every cell in a matrix to zero.
             */
            public void clear()
            {
                for (int i = 0; i < row; i++)
                    for (int j = 0; j < col; j++)
                        matrixData[i][j] = 0;
            }
        
            @Override
            protected Object clone() throws CloneNotSupportedException
            {
                return new Matrix(matrixData);
            }
        
            @Override
            public boolean equals(Object obj)
            {
                if (obj == null)
                {
                    return false;
                }
                if (!(obj instanceof Matrix))
                {
                    return false;
                }
                Matrix that = (Matrix)obj;
                for (int i = 0; i < row; i++)
                    for (int j = 0; j < col; j++)
                    {
                        if (matrixData[i][j] != that.get(i,j))
                            return false;
                    }
        
                return true;
            }
        
            public void print()
            {
                for (int i = 0; i < matrixData.length; i++)
                {
                    for (int j = 0; j < matrixData[i].length; j++)
                    {
                        System.out.print(matrixData[i][j] + ", ");
                    }
                    System.out.println();
                }
            }
        
            /**
             * Gets one column of a matrix object as a new matrix object.
             * 
             * @param col
             * @return
             */
            public Matrix getCol(final int col)
            {
                double[] results = new double[matrixData[col].length];
                for (int i = 0; i < row; i++)
                    results[i] = matrixData[i][col];
        
                return Matrix.createColumnMatrix(results);
            }
        
            /**
             * Gets one row of a matrix object as a new matrix object.
             * 
             * @param row
             * @return
             */
            public Matrix getRow(final int row)
            {
                double[] results = new double[this.row];
                for (int i = 0; i < col; i++)
                    results[i] = matrixData[row][i];
        
                return Matrix.createRowMatrix(results);
            }
        
            /**
             * Returns the sum of every cell in a matrix object.
             * 
             * @return
             */
            public double sum()
            {
                double total = 0;
                for (int i = 0; i < row; i++)
                    for (int j = 0; j < col; j++)
                    {
                        total += matrixData[i][j];
                    }
                return total;
            }
        
            /**
             * Determines if every cell in a matrix object is zero.
             * 
             * @return
             */
            public boolean isZero()
            {
                for (int i = 0; i < row; i++)
                    for (int j = 0; j < col; j++)
                    {
                        if (matrixData[i][j] != 0)
                            return false;
                    }
        
                return true;
            }
        
            /**
             * Get given value at a specific location
             * 
             * @param row
             * @param col
             * @return
             */
            public double get(int row, int col)
            {
                return matrixData[row][col];
            }
        
            public int getCols()
            {
                return col;
            }
        
            public int getRows()
            {
                return row;
            }
        
            public static Matrix identity(final int size)
            {
                final Matrix result = new Matrix(size,size);
        
                for (int i = 0; i < size; i++)
                {
                    result.set(i,i,1);
                }
                return result;
            }
        
            /**
             * Set given row and column using 0 based indexes
             * 
             * @param row
             * @param col
             * @param value
             */
            public void set(int row, int col, double value)
            {
                matrixData[row][col] = value;
            }
        
            /**
             * Get the copy of the current matrix
             * 
             * @return
             */
            public double[][] getRawMatrix()
            {
                double[][] copy = new double[matrixData.length][];
                for (int i = 0; i < matrixData.length; i++)
                {
                    copy[i] = new double[matrixData[i].length];
                    for (int j = 0; j < matrixData[i].length; j++)
                        copy[i][j] = matrixData[i][j];
                }
                return copy;
            }
        
            public static void dump(double[] arr)
            {
                for (int i = 0; i < arr.length; i++)
                {
                    System.out.print(arr[i] + ", ");
                }
            }
        
            public void dump()
            {
                dump(System.out);
            }
        
            public void dump(PrintStream ps)
            {
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < col; j++)
                    {
                        ps.print(matrixData[i][j] + ", ");
                    }
                    ps.println();
                }
                ps.println();
            }
        
            public void add(Matrix contribution)
            {
                // TODO Auto-generated method stub
        
            }
        

        【讨论】:

        • 测试我的代码,布局与此类似,我是否只需在构造函数中指定行和列长度?
        • 您可以执行 'new Matrix(4) 或 new Matrix(2,4)' 或克隆现有矩阵,
        猜你喜欢
        • 2015-03-14
        • 1970-01-01
        • 1970-01-01
        • 2016-03-30
        • 1970-01-01
        • 2012-09-07
        • 2021-10-26
        • 2021-02-05
        • 1970-01-01
        相关资源
        最近更新 更多