在 Java 中将二维数组的列作为参数传递
我只想在方法调用中传递二维数组的列。我知道如何逐行传递 2 d,即
Check(a[i],9);
考虑到 a 被定义为 2 d 数组。
但是我不知道如何逐行执行此操作...不执行此操作时会出现错误
Check(a[i][],9);
谢谢
I want to only pass columns of a 2 d array in a method call. I know how to pass 2 d row by row and that is
Check(a[i],9);
taken in mind that a is defined as a 2 d array.
However I don't know how to do that row by row... not when doing this gives error
Check(a[i][],9);
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你不能以这种方式访问二维数组中的“列”,至少在 Java 中不能。您需要手动迭代行并选择所需的列值。
You cannot access a 'column' in a 2d array in this way, at least not in Java. You would need to manually iterate the rows and select the column value you wanted.
如果不显式创建数组并从原始矩阵逐个元素复制,则无法执行此操作。
You can't do this without explicitly creating an array and copying from your original matrix element by element.
不确定我是否正确理解你的问题,但我认为你错过了第二个循环。
您可能还想要这个(不确定)不知道是否可以编译,其想法是将列值复制到新数组并传递
Not sure I understand your question correctly, but i think you are missing a second loop.
you might also want this (not sure) dunno if this compiles, the idea is to copy the column values to a new array and pass that
二维数组不是矩阵。它的行为更像是数组的数组。
您似乎正在寻找一个由每个内部数组的第一个元素组成的数组,该数组无法自动访问。您需要创建一个新数组。
A 2d array is not a matrix. It behaves more like an array of arrays.
What you appear to be looking for is an array made of the first element of each of the inner arrays, which cannot be accessed automatically. You would need to make a new array.
我认为您需要像这样创建该数组,然后传递它:
I think you would need to create that array like so and then pass it: