Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a list of array into an array of array

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

like image 930
Fili Avatar asked Apr 27 '11 11:04

Fili


People also ask

Can you convert a list to an array?

The best and easiest way to convert a List into an Array in Java is to use the . toArray() method. Likewise, we can convert back a List to Array using the Arrays. asList() method.

Can we convert list to array in Python?

Lists can be converted to arrays using the built-in functions in the Python numpy library. numpy provides us with two functions to use when converting a list into an array: numpy. array()


1 Answers

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

like image 194
Jeremy Avatar answered Oct 13 '22 19:10

Jeremy