1

I wanted to know if it is possible to convert two single dimensional array into one two dimensional array using arraylists.

Here's my code:

String[] user = (String[])compList.toArray(new String[usersList.size()]);

String[] computer = (String[])compList.toArray(new String[compList.size()]);

2 Answers 2

1

I assume

String[] user = (String[])compList.toArray(new String[usersList.size()]);

should be

String[] user = (String[])usersList.toArray(new String[usersList.size()]);

I don't think this is possible, assuming you want something like

String comuterUser[][]

where computerUser[0] is the user and computerUser[1] is the computer. You'll have to iterate through the lists and populate the array. Something like (assuming the two lists are of equal length):

String computerUser[][] = new String[usersList.size()][];
for (int i = 0; i < computerUser.lenth; i++) {
    computerUser[i] = new String[]{ usersList.get(i), compList.get(i) };
}    

Best to have the two lists ArrayLists for speedy lookup. I haven't tested the above, but it should work.

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

2 Comments

i tried the code but when i ran it i got this error: [[Ljava.lang.String;@53343ed0
I missed a square bracket pair in the array declaration. Fixed in the code above. Sorry, I tested it, it works now. Try again.
0

You can combine the arrays using System.arrayCopy.

private String[][] wideArray(String[] zero, String[] one) {
    String[][] combined=new String[2][Math.max(zero.length, one.length)];
    System.arraycopy(zero, 0, combined[0], 0, zero.length);
    System.arraycopy(one, 0, combined[1], 0, one.length);
    return combined;
}

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.