简体   繁体   中英

Converting arrays into a multi-dimensional array - 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:-

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

And should result in:-

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

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.

Thanks to any response.

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];
}
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();
    }
}

Output:

hello1  A2  X19912  
hello2  B2  Y19912  
hello3  C2  Z19912 

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 .

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);
}
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];
}

This creates a multidimensional array that should look like this:

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

a clever way of "toTableFormTheArrays" is:

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);

note: can change types, multi calls of to2darray for other D, maybe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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