转换的ArrayList<对象[]>反对[][] [英] Convert ArrayList<Object[]> to Object[][]

查看:109
本文介绍了转换的ArrayList<对象[]>反对[][]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中,我做转换阵列的ArrayList到一个二维数组?

How, in Java, do I convert an ArrayList of arrays to a two-dimensional array?

例如:

ArrayList<String[]> results = new ArrayList<String[]>();
String [] columns = {a few strings};


JTable table = new JTable(results.toArray(), columns);

我得到的错误的JTable(Object []对象,对象[])没有定义。

I get the error that JTable(Object[], Object[]) is not defined.

推荐答案

的<一个href=\"http://download.oracle.com/javase/6/docs/api/java/util/List.html#toArray%28T%5b%5d%29\"><$c$c>List<T>.toArray(T[])方法应该做的工作。

The List<T>.toArray(T[]) method should do the job.

例如:

List<String[]> list = ...
String[][] res = new String[list.size()][];
list.toArray(res);

List<String[]> list = ...
Object[][] res = new Object[list.size()][];
list.toArray(res);

如果您使用此重载而不是列表&LT; T&GT; .toArray()超载,你可以选择的实际类型的数组。这需要code的一个额外的行,但是如果数组类型是显著,这是做它的方式。

If you use this overload rather than the List<T>.toArray() overload, you can choose the actual type of the array. It takes one extra line of code, but if the array type is significant, this is the way to do it.

(即列表&LT; T&GT; .toArray()重载给你一个数组,其实际类型是对象[] ... 不论的泛型类型列表,或列表中的元素的实际类型(S)的。)

(The List<T>.toArray() overload gives you an array whose actual type is Object[] ... irrespective of the generic type of the list, or the actual type(s) of the list elements.)

这篇关于转换的ArrayList&LT;对象[]&GT;反对[][]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆