[英]Array to row on 2D List/ArrayList Java
I'm wanting to create a 2D array with a set amount columns (5) but an indefinite amount of rows. 我想创建一个2D数组,其列数设置为(5),但行数不确定。 It was suggested to use a list with an arraylist like so
List<List<String>> _upload = new ArrayList<List<String>>();
建议使用带有数组列表的列表,例如
List<List<String>> _upload = new ArrayList<List<String>>();
However this doesn't give me my 5 set columns I need. 但是,这并没有给我我需要的5个设置列。
My second problem is the way I'm feeding data into this 2D array. 我的第二个问题是将数据馈入2D数组的方式。 I'm given an array of information (5 long to fit a row)
String[] _ToUpload = {"One", "Two","Three","Four","Five"};
我得到了一个信息数组(长度为5,可以容纳一行)
String[] _ToUpload = {"One", "Two","Three","Four","Five"};
. 。 How would go about implementing a method that created a new row and merged that array into the row of the 2D array.
如何实现创建新行并将该数组合并到2D数组的行中的方法。
New to Java so sorry if this seems a dumb question. Java的新手,如果这是一个愚蠢的问题,对不起。 Many thanks
非常感谢
Try this(I haven't tested it) : 试试这个(我还没有测试):
// create an list named arrayList and add elements to the list
String[][] array = new String[arrayList.size()][];
for (int i = 0; i < arrayList.size(); i++) {
ArrayList<String> row = arrayList.get(i);
array[i] = row.toArray(new String[row.size()]);
}
决定按照建议制作对象的ArrayList。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.