0

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.

3
  • Show the code which initializing the three variables and How can i create the array so it creates an array means you are trying to create the array data ? Commented Aug 15, 2015 at 0:11
  • Are you asking how you initialize a 2d array, or how you programmatically set values into elements in a 2d array? Commented Aug 15, 2015 at 0:13
  • You need to show the exact code that you're trying because your question does not make a lot of sense. Commented Aug 15, 2015 at 1:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is much more like what i am looking for, let me try and apply it to my scenario, this is exactly the logic im looking for. Il get back to you on how i go and give you the rating once i have applied it. many thanks. I searched the web for the proof link you sent, with no luck, thanks for this link its very very useful :D
I'm glad I could help

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.