在 Java 中将二维数组的列作为参数传递

发布于 2025-01-08 00:12:56 字数 190 浏览 0 评论 0原文

我只想在方法调用中传递二维数组的列。我知道如何逐行传递 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

塔塔猫 2025-01-15 00:12:57

你不能以这种方式访问​​二维数组中的“列”,至少在 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.

娜些时光,永不杰束 2025-01-15 00:12:57

如果不显式创建数组并从原始矩阵逐个元素复制,则无法执行此操作。

You can't do this without explicitly creating an array and copying from your original matrix element by element.

在巴黎塔顶看东京樱花 2025-01-15 00:12:57

不确定我是否正确理解你的问题,但我认为你错过了第二个循环。

for( int i = 0; i < a.length; i++ )
{
   for( int j = 0; j < a[i].length; j++ )
   {
      cellAtRowIColumnJ(a[i][j], 9) //what is the 9 for?
   } 
}

您可能还想要这个(不确定)不知道是否可以编译,其想法是将列值复制到新数组并传递

int[] cols = new int[a.length];
for( int i = 0; i < a.length; i++ )
{
   cols[i] = a[i][9];
}
callWithColumns(cols);

Not sure I understand your question correctly, but i think you are missing a second loop.

for( int i = 0; i < a.length; i++ )
{
   for( int j = 0; j < a[i].length; j++ )
   {
      cellAtRowIColumnJ(a[i][j], 9) //what is the 9 for?
   } 
}

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

int[] cols = new int[a.length];
for( int i = 0; i < a.length; i++ )
{
   cols[i] = a[i][9];
}
callWithColumns(cols);
≈。彩虹 2025-01-15 00:12:57

二维数组不是矩阵。它的行为更像是数组的数组。

int a[][];
for (int b[] : a)
    for (int c : b)
        System.out.print(c);

您似乎正在寻找一个由每个内部数组的第一个元素组成的数组,该数组无法自动访问。您需要创建一个新数组。

int temp[] = new int[a.length];
for (int x = 0; x < temp.length; x++)
    temp[x] = a[x][0];

A 2d array is not a matrix. It behaves more like an array of arrays.

int a[][];
for (int b[] : a)
    for (int c : b)
        System.out.print(c);

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.

int temp[] = new int[a.length];
for (int x = 0; x < temp.length; x++)
    temp[x] = a[x][0];
作妖 2025-01-15 00:12:57

我认为您需要像这样创建该数组,然后传递它:

int column = 0; // column you want to get
int[] col = new int[a.length];
for(int i = 0; i < a.length; i++) {
    col[i] = a[i][column];
}

// col is now what you want to pass.
Check(col, 9);

I think you would need to create that array like so and then pass it:

int column = 0; // column you want to get
int[] col = new int[a.length];
for(int i = 0; i < a.length; i++) {
    col[i] = a[i][column];
}

// col is now what you want to pass.
Check(col, 9);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文