将数组转换为多维数组-Java [英] Converting arrays into a multi-dimensional array - Java

查看:60
本文介绍了将数组转换为多维数组-Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

is there a way to convert three single dimensional arrays into a multi-dimensional one. For example I have three arrays (text, retweets, geo) how can I merge them so it appears as:-

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

The arrays I want to merge are something along the lines of text = 'hello, 'hello'. retweets = '2,5' and geo = '19912, 929293'.

并且应该导致:-

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

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

and so one... All of the arrays are the same sizes. I know I should loop through while a for loop somehow but am not quite sure how to implement it.

感谢您的任何回应.

推荐答案

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的所有元素插入2d数组中.

This code will first copy the given arrays into a new array. Then it will insert all elements of copyArrays into a 2d array using for loop .

这篇关于将数组转换为多维数组-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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