19

I have a list like this:

List<MyObject[]> list= new LinkedList<MyObject[]>();

and on Object like this:

MyObject[][] myMatrix;

How can I assign the "list" to "myMatrix"?

I don't want to loop over the list and assign element by element to MyMatrix, but I want to assign it directly (with the oppurtune modifications) if possible. Thanks

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
Fili
  • 193
  • 1
  • 1
  • 4
  • [stackoverflow.com...fill-a-array-with-list-data](http://stackoverflow.com/questions/2538718/fill-a-array-with-list-data) is a nearly a duplicate – Lars Apr 27 '11 at 12:07

6 Answers6

22

You could use toArray(T[]).

import java.util.*;
public class Test{
    public static void main(String[] a){ 
        List<String[]> list=new ArrayList<String[]>();
        String[][] matrix=new String[list.size()][];
        matrix=list.toArray(matrix);
    }   
}

Javadoc

Jeremy
  • 22,188
  • 4
  • 68
  • 81
  • It doesn't compile: "incompatible types; found: array MyObject[], required: array MyObject[][]" – Fili Apr 27 '11 at 12:13
  • 1
    Actually, try it with a 0 by 0 matrix and it should still work ;) – Gressie Apr 27 '11 at 12:15
  • you don't need to know the size of the matrix: see for example my code [here](http://stackoverflow.com/questions/5803619/java-convert-a-list-of-array-into-an-array-of-array/5803952#5803952) – MarcoS Apr 27 '11 at 12:22
  • I tried this solution even with some modification to the size of the matrix, but it doesn't compile :( – Fili Apr 27 '11 at 12:23
  • I knew I shouldn't have assumed modifying it real quick would break the compile. Fixed. – Jeremy Apr 27 '11 at 12:35
  • 1
    Thanks for the solution but it didn't compile. At end I solved with a loop. – Fili Apr 27 '11 at 12:42
9

The following snippet shows a solution:

// create a linked list
List<String[]> arrays = new LinkedList<String[]>();

// add some trivial test data (note: arrays with different lengths)
arrays.add(new String[]{"a", "b", "c"});
arrays.add(new String[]{"d", "e", "f", "g"});

// convert the datastructure to a 2D array
String[][] matrix = arrays.toArray(new String[0][]);

// test output of the 2D array
for (String[] s:matrix)
  System.out.println(Arrays.toString(s));

Try it on ideone

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
1

Let us assume that we have a list of 'int' array.

List<int[]> list = new ArrayList();

Now to convert it into 2D array of type 'int', we use 'toArray()' method.

int result[][] = list.toArray(new int[list.size()][]);

We can generalize it further like-

List<T[]> list = new ArrayList();
T result[][] = list.toArray(new T[list.size()][]);

Here, T is the type of array.

0

Use toArray() or toArray(T[]) method of LinkedList.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • With toArray() the editor says "incompatible types; found: array java.lang.Object[], required: array Item[][]". I tried to cast the result of toArray() but it give me a ClassCastException – Fili Apr 27 '11 at 12:00
  • 2
    Did you try the toArray(T []) ? – Gressie Apr 27 '11 at 12:04
  • @Gressie: +1, good suggestion. By this @Fili might be able to do it. – Harry Joy Apr 27 '11 at 12:05
0

You can do it like this:

public static void main(String[] args) {
    List<Item[]> itemLists = new ArrayList<Item[]>();
    itemLists.add(new Item[] {new Item("foo"), new Item("bar")});
    itemLists.add(new Item[] {new Item("f"), new Item("o"), new Item("o")});
    Item[][] itemMatrix = itemLists.toArray(new Item[0][0]);
    for (int i = 0; i < itemMatrix.length; i++)
        System.out.println(Arrays.toString(itemMatrix[i]));
}

Output is

[Item [name=foo], Item [name=bar]]
[Item [name=f], Item [name=o], Item [name=o]]

assuming that Item is as follows:

public class Item {

    private String name;

    public Item(String name) {
        super();
        this.name = name;
    }

    @Override
    public String toString() {
        return "Item [name=" + name + "]";
    }

}
MarcoS
  • 13,386
  • 7
  • 42
  • 63
0

Converto list to array using. List.Array()

Then Use System.arraycopy to copy to the 2d array works well for me

Object[][] destination = new Object[source.size()][];

System.arraycopy(source, 0, destination, 0, source.size());
mindlid
  • 1,679
  • 14
  • 17