【问题标题】:Converting arrays into a multi-dimensional array - Java将数组转换为多维数组 - Java
【发布时间】:2013-01-20 12:20:54
【问题描述】:

有没有办法将三个一维数组转换成一个多维数组。 例如,我有三个数组(文本、转发、地理)如何合并它们以使其显示为:-

我要合并的数组类似于 text = 'hello, 'hello'。转推 = '2,5' 和 geo = '19912, 929293'。

并且应该导致:-

combined = 
[hello, 2, 19912
hello, 5, 929293]

等等...所有数组的大小都相同。我知道我应该以某种方式循环 while 循环,但不太确定如何实现它。

感谢任何回复。

【问题讨论】:

  • 我尝试过使用 ArrayList,但无济于事

标签: java arrays loops for-loop multidimensional-array


【解决方案1】:
int count = ...;

String [] text = new String [count];
int [] retweets = new int [count];
int [] geo = new int [count];

// Fill arrays with data here

Object [] combined = new Object [count * 3];

for (int i = 0, j = 0; i < count; i++)
{
    combined [j++] = text [i];
    combined [j++] = retweets [i];
    combined [j++] = geo [i];
}

【讨论】:

  • 这不是多维数组。
【解决方案2】:
public static void main(String[] args) {

    String[] array1 = { "hello1", "A2", "X19912" };
    String[] array2 = { "hello2", "B2", "Y19912" };
    String[] array3 = { "hello3", "C2", "Z19912" };
    String[] copyArrays = new String[array1.length + array2.length
            + array3.length];
    System.arraycopy(array1, 0, copyArrays, 0, array1.length);
    System.arraycopy(array2, 0, copyArrays, array1.length, array2.length);
    System.arraycopy(array3, 0, copyArrays, array1.length + array2.length,
            array3.length);

    String[][] array = new String[3][3];
    int index = 0;
    for (int i = 0; i < array.length; i++)
        for (int j = 0; j < array[i].length; j++) {
            array[i][j] = copyArrays[index++];
        }

    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array[i].length; j++) {

            System.out.print(array[i][j] + "  ");
        }
        System.out.println();
    }
}

输出:

hello1  A2  X19912  
hello2  B2  Y19912  
hello3  C2  Z19912 

此代码将首先将给定的数组复制到一个新数组中。然后它将使用 for 循环将 copyArrays 的所有元素插入到二维数组中。

【讨论】:

  • 嘿,谢谢,这符合我想要的,但方向错误。检查我对原始帖子的更新。感谢您的帮助。
  • @user1840255 你试过我的代码了吗?您希望数据以表格形式呈现...是吗?表表示行和列,我的答案是生成 2d 。首先它将给定的数组复制到一个数组中,然后将其插入到二维数组中。
【解决方案3】:
String[] text =...;
int[] retweets =...;
int[] geo =...;

int len = text.length;

List<List<Object>> items = new ArrayList<>();
for (int i = 0; i < len; i++) {
   List<Object> item = new ArrayList<>();
   item.add(text[i]);
   item.add(retweets[i]);
   item.add(geo[i]);
   items.add(item);
}

【讨论】:

    【解决方案4】:

    “toTableFormTheArrays”的一个巧妙方法是:

    public static String[][] to2DArray(String[]... yourArrays){
        return yourArrays;
    }
    
    then the code is:
    String[] text =  {"hello", "hello"};
    String[] retweets = {2, 5};
    String[] geo = {19912, 929293};
    
    String[][] yourTableForm2DArray = to2DArray(text, retweets, geo);
    

    注意:可以改变类型,对其他D多调用to2darray,也许吧。

    【讨论】:

      【解决方案5】:
      String[] text =  {"hello", "hello"};
      int[] retweets = {2, 5};
      int[] geo = {19912, 929293};
      
      //create table of strings for each array
      Object[][] combined = new Object[text.length][3];
      
      //load information into table, converting all information into Strings
      for(int row = 0; row<text.length; row++){
          combined[row][0] = text[row];
          combined[row][1] = retweets[row];
          combined[row][2] = geo[row];
      }
      

      这将创建一个如下所示的多维数组:

      [[hello, 2, 19912], [hello, 5, 929293 ]]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-06
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 2019-06-29
        相关资源
        最近更新 更多