如何检查二维数组的行数和列数,以便在循环中检查大小是否合适。

huangapple go评论100阅读模式
英文:

How to check the amount of rows and columns in a 2D array for a loop checking if the size is appropriate

问题

目前我正在处理一个涉及二维数组的任务。在任务的某个特定部分,我需要创建一个构造函数,执行以下详细任务:

(1)完成 Matrix 类的第二个构造函数 "Matrix(int r, int c, double v[][])"
创建一个具有 "r" 行和 "c" 列的矩阵,并将元素赋值为第三个参数 "v[]",即一个双重的二维数组。您需要进行一些检查,以确保输入的二维数组的行和列与前两个输入参数的值相同或更大。如果输入的二维数组包含的数字比所需的 "r" 行和 "c" 列更多,那也是可以的。您可以简单地忽略那些额外的数字。(提示:要检查数组的大小,可以使用数组对象的 "length" 成员)

我目前的代码如下:

Matrix(int r, int c, double v[][]) {
    // 在这里实现...
    rows = r;
    cols = c;
}

我已经考虑过应该添加 values=v;,然后使用 .length 来检查数组。但这是我目前卡住的一部分,因为我不确定如何分别检查二维数组的两个维度。一旦我解决了这一部分,我计划编写一个 if 语句,首先检查 r 是否小于或等于数组的第一个长度值,然后检查 c 是否小于或等于数组的第二个长度值。非常感谢您提供帮助来填补空白部分或检查我已有的内容。

英文:

Currently I am working on an assignment that works with a 2D array. For a certain part of the assignment, I have to create a constructor that performs a variety of tasks detailed here:

(1) Finish the second constructor of Matrix class “Matrix(int r, int c, double v[][])”
Create the matrix with “r” rows and “c” columns and assign the elements with the third
argument "v[]", which is a double 2D array. You need to do some checking to make sure
the input 2D array should have the rows and columns with the same values as or
greater values than the first two input arguments. It is fine if the input 2D array contains
more numbers than the specified “r” rows and “c” columns needed. You can simply
ignore those extra numbers. (Hint: to check the size of the array, use the "length" data
member of an array object)

The code I have so far is as follows:

Matrix(int r, int c, double v[][]) {
            //Implementation here...
            rows = r;
            cols = c;
          
            
	}

I'm already thinking that I should add values=v;, then using .length to check the array somehow. This is one part I'm stuck on, as I'm not sure how to check both of the 2D array's dimensions separately. When I get this part, I'm planning on simply writing an if statement that first checks if r is less than or equal to the first length value of the array, then checking if c is less than or equal to the second length of the array. Any help filling in the blanks or checking what I have would be much appreciated.

答案1

得分: 1

假设所有列的长度相同,您可以使用:

int rows = v.length;
int cols = v[0].length;

然后您可以检查 rc 是否等于或大于分别等于 rowscols

英文:

Assuming all columns are of same length, You can use:

int rows = v.length;
int cols = v[0].length;

And then you can check if r & c are equal to or greater than rows & cols respectively.

答案2

得分: 1

问题的解决方案已经给出,以下是其背后的工作原理。

一个二维数组实际上是一个指针数组,其中每个指针可以引用一个一维数组。这些一维数组是二维数组的行。
例如:int A[][] = new int[3][4]

因此,当你写 A.length 时,它给出的是行的长度。当你明确写出 A[0].length 时,它会返回在 A[0] 位置的数组的长度,即数组的列。

英文:

The solution to the problem is already given, This is how it works behind the scene.

A 2D array is really an array of pointers, where each pointer can refer to a one-dimensional array. Those one-dimensional arrays are the rows of the 2D array
eg: int A[][] = new int[3][4]
如何检查二维数组的行数和列数,以便在循环中检查大小是否合适。

So when you write A.length it gives you the length of the rows. and when you specifically write A[0].length it says give me the length of the array on A[0] position.
which is the column of the array.

huangapple
  • 本文由 发表于 2020年9月26日 02:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64069603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定