Convert ArrayList to Object [] [] [closed]

1

How to convert an ArrayList to a 2D Object.

My Current Code:

ArrayList<String> a = null;
    a = new ArrayList<String>();                    
            a.add("ABC");
            a.add("DEF");
            a.add("1");
            a.add("1");

//Converter para Object 2D
int n = a.size();
System.out.println("Tamanho do Array: "+n);
Object[][] data = new Object[n][];
for (int i = 0; i < n; i++) 
    data[i] =    a.toArray();

This way I can get the first value of ArrayList, but it repeats itself in all positions, I think the reason is because I'm not requesting other arraylist positions in line data[i] = a.toArray(); , I tried to change that line to data[i] = a.get(i); but in this case I get the error " Type mismatch: can not convert from String to Object [] "

    
asked by anonymous 15.12.2017 / 14:25

1 answer

0
ArrayList<String> a = null;
a = new ArrayList<String>();
a.add("ABC");
a.add("DEF");
a.add("1");
a.add("1");        

//Converter para Object 2D
int n = (a.size() + 1) / 2;
System.out.println("Tamanho do Array: " + n);
Object[][] data = new Object[n][n];
int temp = 0;
for (int i = 0; i < data.length; i++) {
    for (int j = 0; j < data.length; j++) {
        data[i][j] = a.get(temp);
        temp++;
    }
}

for (int i = 0; i < data.length; i++) {
    System.out.print("[");
    for (int j = 0; j < data.length; j++) {
        System.out.print(" " + data[i][j] + "");
    }
    System.out.println("]");
}
    
15.12.2017 / 14:46