简体   繁体   English

在java中用两个1d数组填充2d数组

[英]populating 2d array with two 1d arrays in java

I have 2 1d arrays and im trying to populate them into a single 2d array in JAVA. 我有2个1d数组,我试图将它们填充到JAVA中的单个2d数组中。

For instance: 例如:

x[] = {2,5,7,9}
y[] = {11,22,33,44}

The results should then be: 结果应该是:

result[][] = {{2,5,7,9}, {11,22,33,44}}

How do I go about this? 我该怎么做? I currently have something like this: 我目前有这样的事情:

for(int row = 0; row < 2; row++) {
    for(int col = 0; col == y.length; col++) {
        ???
    }
}

Im sort of stuck from there... 我有点卡在那里......

2D array is an array of arrays. 2D数组是一个数组数组。 So why don't you try this? 那么你为什么不尝试这个呢?

int result[][] = {x,y};

And to make sure that it is so simple and works, test this: 并确保它如此简单和有效,测试这个:

for(int i=0; i<result.length; i++)
{
    for(int j=0; j<result[0].length; j++)
        System.out.print(result[i][j]+ " ");
    System.out.println();
}

Try this: 试试这个:

ArrayList<Integer[]> tempList = new ArrayList<Integer[]>();

tempList.add(x);
tempList.add(y);

Integer result[][] = new Integer[tempList.size()][];
result = tempList.toArray(tempList);

You have to revert the row and column indices 您必须还原行和列索引

for(int row = 0; row < 2; row++)
{
    for(int col = 0; col = y.length; col++)
    {
        ....
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM