转换 ArrayList<Object[]>反对[][] [英] Convert ArrayList&lt;Object[]&gt; to Object[][]

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

问题描述

在 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[], Object[]) 的错误.

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

推荐答案

List.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);

如果使用此重载而不是 List.toArray() 重载,则可以选择数组的实际类型.这需要额外的一行代码,但如果数组类型很重要,这就是这样做的方法.

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.

(List.toArray() 重载给你一个数组,它的实际类型是 Object[] ... independent列表的泛型类型,或列表元素的实际类型.)

(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<Object[]>反对[][]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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