0

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.

1
  • I tried mucking around with ArrayList's but to no avail Commented Feb 5, 2013 at 14:31

5 Answers 5

2
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];
}
Sign up to request clarification or add additional context in comments.

1 Comment

this is not a multidimensional array.
1
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 .

2 Comments

Hey Thanks this is along the lines of what I want but the wrong way round. Check my update to the original post. Thanks for your help.
@user1840255 You tried my code? You want data to be in table like form ...yes? Table means row & column and my answer is generating 2d . First it copy the given arrays into one and then insert it into 2d array.
1
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);
}

Comments

1

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.

Comments

0
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 ]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.