Java 二维数组表示法和默认访问

标签 java .net arrays

来自以下代码:

   /** Array for internal storage of elements.
   @serial internal array storage.
   */
   private double[][] A;

   /** Row and column dimensions.
   @serial row dimension.
   @serial column dimension.
   */
   private int m, n;

public Matrix times (Matrix B) {
      if (B.m != n) {
         throw new IllegalArgumentException("Matrix inner dimensions must agree.");
      }
      Matrix X = new Matrix(m,B.n);
      double[][] C = X.getArray();
      double[] Bcolj = new double[n];
      for (int j = 0; j < B.n; j++) {
         for (int k = 0; k < n; k++) {
            Bcolj[k] = B.A[k][j];
         }
         for (int i = 0; i < m; i++) {
            double[] Arowi = A[i];
            double s = 0;
            for (int k = 0; k < n; k++) {
               s += Arowi[k]*Bcolj[k];
            }
            C[i][j] = s;
         }
      }
      return X;
   }

double[] Arowi = A[i];

这条线在做什么? A是二维Matrix,为什么允许A[i]?它到底在做什么? 这和A[i][0]一样吗?

这种表示法令人困惑。

还有人可以将该行转换为 .NET,您将如何使用 double[,] Matrix 来实现这一点?

我知道有一个 Matrix.GetLength() 可以为您提供指定的维度长度。 但与那行 Java 代码相比,它会如何翻译呢?

编辑 我相信我通过替换该行并传入我的矩阵以将第一行复制到新的一维矩阵并返回它来修复它

 public double[] ConvertFirstRowOfMultiArrayToOneArray(double[,] p)
        {
            double[] array = new double[p.Length];
            for (int i = 0; i < p.GetLength(0); i++)
            {
                array[i] = p[0, i];
            }

            return array;
        }

最佳答案

What is this line doing?

它将声明为一维数组的 Arowi 设置为数组 A[i]

假设这种情况:

double[][] A = new double[2][2];
A[0][0] = 1.5;
A[0][1] = 2.5;
A[1][0] = 3.5;
A[1][0] = 4.5;

double[] B = A[0]; // Imagine A[0] as A[i]

/* then value of B is now A[0]
so B is now a unidimensional array containing value:
B[0] == 1.5 == A[0][0]
B[1] == 2.5 == A[0][1] */

A is a two dimensional Matrix, why is A[i] allowed?

因为如您所见,您将一维数组值 (B||Arowi) 设置为包含 A[i] 数组的第二个维度。

想象 A[i] 本身包含另一个数组(二维)。所以我们将这个维度保存到一个新变量中。在这种情况下,如果我没有感到困惑的话,它将引用内存地址,而不是值本身,并且例如,如果您在 Arowi||B 中编辑值,它将在原始数组中发生变化。

示例:

B[0] == 1.5 == TRUE
B[0] = 1.75
A[0][0] == 1.5 == FALSE
A[0][0] == 1.75 == TRUE

and what exactly is it doing?

我在示例中对此进行了解释。

如果我没有感到困惑的话,应用程序正在执行以下操作:

保存A数组(类(A)中当前数组成员/属性)相乘后的结果(加运算,s += Arowi[k]*Bcolj[k])或者可能是 Matrix 类本身,我看不到所有代码)通过 B 列的值(参数采用的 Matrix 对象)。在每个主循环中,它都会遍历 B 中的一个新列,存储该列中行的所有值,然后循环整个 A 数组或类数组,将结果存储在 C 中,然后将其作为 Matrix 对象返回。我希望这是可以理解的并且我不会感到困惑。

Is that the same thing as A[i][0]?

没有

A[i][0] 设置值。 A[i] 设置一维数组

抱歉我的英语不好,我希望你能理解并且描述和想法是正确的。

编辑

看例子:

static void Main(string[] args)
    {
        double[][] A = new double[2][] { new double[2], new double[2] };
        A[0][0] = 1.5;
        A[0][1] = 2.5;
        A[1][0] = 3.5;
        A[1][1] = 4.5;
        System.Console.WriteLine(A[0][0]);
        // Prints 1,5
        Console.Read();
        double[] B = A[0];
        System.Console.WriteLine(B[0]);
        // Prints 1,5
        System.Console.Read();
        System.Console.Read();

    }

因此,double[] Arowi = A[i]; 行将转换为 double[] Arowi = A[i];,完全相同。

只需重新创建锯齿状数组 double[][] A 而不是像 double[,] A 这样的数组,因为似乎不是引用没有锯齿状数组的维度的方法(A[m,n];double[] Arowi = A[i]; 不可能像在 Java 中一样完成)。我现在还不能确切地说出为什么。刚刚在我的 VS2013 中尝试过。

我建议您查看MSDN文档:http://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx

我还向您推荐这个帖子:Multidimensional Array [][] vs [,]

关于Java 二维数组表示法和默认访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28005021/

相关文章:

java - 有没有办法在服务器端的 node.js 中运行 Java Applet?

.net - 哪些编程实践会影响窗口句柄的数量?

java - 字符串 B 字赋值

arrays - Joi 验证正则表达式或模式

java - Pattern.quote 未按预期工作

JavaScript - 以及创建和使用 Java 对象

java - 使用日历开始日期和结束日期填充 jtable

.net - Outlook 约会功能区按钮事件

c# - 退出后保留程序的数据

php - 如何检查一个值是否大于数组中的值