Ingram
Ingram

Reputation: 674

creating an array from variables

I have an array as follows

Object[][] data = {assd, eventy,assFT}

assd, eventy and assFT are variables and hold different values, all three variables hold many values. The variables are fed through from a loop.

How can i create the array so it creates an array like this

Object[][] data = {{assd, eventy,assFT},
                   {assd, eventy,assFT}}

which would represent say for example

Object[][] data = {{hello, goodbye,salut},
                   {hallo, ,gutentag,aurevoir}}

Currently what i am getting by having

Object[][] data = {assd, eventy,assFT}

is

Object[][] data = {hello hallo, goodbye gutentag,salut aurevoir} 

ie i am getting each variable adding to the same index of the first row of the array.

Upvotes: 0

Views: 80

Answers (1)

bpgeck
bpgeck

Reputation: 1624

You would need a method that parses the assd, eventy, and assFT arrays and then reinsert them into a new two dimensional array in the correct format like so:

Object[][] combineArrays(Object[] assd, Object[] eventy, Object[] assFT)
{
    Object[][] outputArray = new Object[3][];
    outputArray[0] = new Object[assd.length];
    outputArray[1] = new Object[eventy.length];
    outputArray[2] = new Object[assFT.length];

    for (int i = 0; i < assd.length; i++)
    {
        outputArray[i][0] = assd[i];
    }

    for (int i = 0; i < assd.length; i++)
    {
        outputArray[i][1] = eventy[i];
    }

    for (int i = 0; i < assd.length; i++)
    {
        outputArray[i][2] = assFT[i];
    }

    return outputArray;
}

See here for proof that this works. I did use ints though instead of Objects though to make it easy on Ideone.

Sorry it took so long to answer your question. I saw it a while ago and didn't have time to type up my answer before leaving my computer.

Upvotes: 1

Related Questions