【问题标题】:Need help creating an array需要帮助创建数组
【发布时间】:2013-12-08 13:45:40
【问题描述】:

我需要一些帮助才能开始。 我有一个数组存储几个数组(在这种情况下,假设是 3,这可能或多或少):

mainarray {arr, arr, arr}

mainarray 中的所有数组都具有相同数量的值,但这也可能会更改为更少或更少 假设我有以下数组:

mainarray {
  arr{"1","2","3"};
  arr{"One","Two","Three"};
  arr{"Red","Blue","Yellow"};
}

每次我尝试制作这个 Object[][] 时,排序结果如下:

Object {{"1","2","3"}, {"One","Two","Three"}, {"Red","Blue","Yellow"}};

我的问题来了: 有没有办法把这个数组变成一个Object[][],但是用这个排序?

Object {{"1","One","Red"}, {"2","Two","Blue"}, {"3","Three","Yellow"}};

提前致谢!

【问题讨论】:

  • 您可以使用几个循环来转换数组的排列。你试过什么?

标签: java arrays object multidimensional-array jtable


【解决方案1】:

给你

    Object arr1[] = { "1", "2", "3" };
    Object arr2[] = { "One", "Two", "Three" };
    Object arr3[] = { "Red", "Blue", "Yellow" };
    //make an array of arrays
    Object allArr[] = { arr1, arr2, arr3 };
    //the new array to store values
    Object arr4[][] = new Object[3][3];

    for (int i = 0; i < 3; i++) {
        for (int j = 0, k = 0; j < 3 && k < 3; j++, k++) {
            //take the first array from array of arrays
            Object tmp[] = (Object[]) allArr[k];
            //take the ith element of each individual array and store in the new array in correct position 
            arr4[i][j] = tmp[i];

        }
    }
     //print the new array
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            System.out.print(arr4[i][j] + " ");
        }
        System.out.println();
    }

这适用于您的示例。您可以进一步概括它。

输出

1 One Red 
2 Two Blue 
3 Three Yellow 

【讨论】:

  • &lt; 3 更改为&lt; allArr.length 并将new Object[3][3] 更改为new Object [arr1.length][allArr.length]。使其适用于所有大小的数组。
  • @mikeaworski 让 OP 这样做 :-) 感谢您的建议
【解决方案2】:

代码

此代码适用于所有大小的数组,假设它们的大小相同(如您所说)。
这只是n1234代码的修改版。

Object arr1[] = { "1", "2", "3"};
Object arr2[] = { "One", "Two", "Three"};
Object arr3[] = { "Red", "Blue", "Yellow"};

Object allArr[] = { arr1, arr2, arr3 }; // combination of the arrays

Object arr4[][] = new Object[arr1.length][allArr.length]; // array to be rearranged

// define the new, rearranged array
for (int i = 0; i < arr4.length; i++) {
    for (int j = 0, k = 0; j < arr4[i].length && k < arr4[i].length; j++, k++) 
    {
        Object tmp[] = (Object[]) allArr[k];
        arr4[i][j] = tmp[i];
    }
}       

// print the array
for (Object r[] : arr4) {
    for (Object c : r)
        System.out.print(c + " ");
    System.out.println("");
}

输入/输出

输入:

Object arr1[] = { "1", "2", "3", "4"};
Object arr2[] = { "One", "Two", "Three", "Four"};
Object arr3[] = { "Red", "Blue", "Yellow", "Green"};

输出:

1 一红
2 两个蓝色
3 三黄


输入:

Object arr1[] = { "1", "2", "3"};
Object arr2[] = { "One", "Two", "Three"};
Object arr3[] = { "Red", "Blue", "Yellow"};

输出:

1 一红
2 两个蓝色
3 三黄
4 四绿

【讨论】:

  • 你本可以为 OP 留下一些工作 :-)。它与我的代码基本相同:-)
  • @n1234 我还向他展示了如何使用其他版本的 for 循环,它更便于打印。而且我认为我应该给他所有他要求的东西,考虑到他说阵列大小可能不同。但是,是的,它只是您的修改版本。我应该给你功劳。
  • 感谢您的回答!两者都是正确的,但我感谢@n1234,因为他是第一个出来的:)
猜你喜欢
  • 1970-01-01
  • 2011-03-08
  • 1970-01-01
  • 2020-03-06
  • 2019-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
相关资源
最近更新 更多