如何从一个2D Java数组列? [英] How to get a column from a 2D java array?

查看:225
本文介绍了如何从一个2D Java数组列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,二维数组是数组的数组。为了得到一个排,你可以做:

I know that 2d arrays are arrays of arrays. To get a row you can do:

rowArray = my2Darray[row]

由于各行可以具有不同的尺寸,我假设它没有内置于获得一个二维数组的列。这使我相信,你必须做一些事情,如:

Since each row can be a different size, I'm assuming it's not built in to get a column from a 2D array. It leads me to believe you'd have to do something like:

for(int row = 0; row < numRows; row++)
{
    colArray[row] = m2Darray[row][columnOfInterest];
}

这是正确的?它是唯一的出路?

Is this correct? Is it the only way?

推荐答案

如果您被锁定到使用二维数组,那么是的,这是它AFAIK。然而,一个建议,可以帮助你(如果可能):

If you are locked down to using a 2d array, then yes, this is it afaik. However, a suggestion that may help you (if possible):

裹在处理列取一个类的数组。

Wrap the array in a class that handles the column fetching.

祝你好运。

这篇关于如何从一个2D Java数组列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆