[英]How to copy elements from `2d array` to `Arraylist`?
folks, what is the technique of copying elements from array
to Arraylist
?伙计们,将元素从
array
复制到Arraylist
的技术是什么?
public DenseBoard(T[][] x, T fillElem){
ArrayList<ArrayList<T>> myBoard = new ArrayList<ArrayList<T>>();
for(int i = 0; i < x.length; i++){
for(int j = 0; j < x[0].length; j++){
myBoard.get(i).add(j); //<<------ getting error!
}
}
}
You'll need to initialize each member of myBoard in the outer loop:您需要在外循环中初始化 myBoard 的每个成员:
public DenseBoard(T[][] x, T fillElem){
ArrayList<ArrayList<T>> myBoard = new ArrayList<ArrayList<T>>();
for(int i = 0; i < x.length; i++){
myBoard.add(new ArrayList<T>); //Gotta add something to stick stuff in
for(int j = 0; j < x[0].length; j++){
myBoard.get(i).add(j); //<<------ no more error?
}
}
}
Since myBoard is an ArrayList of ArrayLists of Ts, we need to give it somewhere to put the T's.由于 myBoard 是 Ts 的 ArrayLists 的 ArrayList,我们需要给它某处放置 T 的地方。 Initially, myBoard looks like this:
最初,myBoard 看起来像这样:
[] <-- empty ArrayList
So we give it somewhere to put data for each row, like this所以我们给它某处放置每一行的数据,像这样
myBoard.add(new ArrayList<T>);
Now it looks like现在看起来像
[ [] ] <--- ArrayList with an empty ArrayList in it, ready to accept T's
We add some T's, and end up with this我们添加一些 T,最后得到这个
[ [T1, T2, T3] ].
And on the next iteration, we'll end up with something like this在下一次迭代中,我们最终会得到这样的结果
[ [T1, T2, T3], [T4, T5, T6] ]
Hope that cleared things up.希望能解决问题。
You are getting error because your are trying to get()
a value from ArrayList
but not had inserted any value first.您收到错误是因为您试图从
ArrayList
get()
一个值,但没有先插入任何值。 To do it your way, here is the correct code :按照你的方式去做,这里是正确的代码:
ArrayList<ArrayList<T>> myBoard = new ArrayList<>();
for(int i = 0; i < x.length; i++){
ArrayList<T> values = new ArrayList<>();
for(int j = 0; j < x[0].length; j++){
values.add(x[i][j]);
}
myBoard.add(values);
}
Just use the following function :只需使用以下功能:
Arrays.asList(T...a)
In your case it will be done as :在您的情况下,它将按照以下方式完成:
ArrayList<T> myBoard = new ArrayList<>();
for(T[] arr : x){
myBoard.add(Arrays.asList(arr));
}
diamond operator <>
to make code more readablediamond operator <>
使代码更具可读性for-each
statement where you couldfor-each
语句Inspired from this post .灵感来自这篇文章。
new ArrayList<Item>(Arrays.asList(array))
creates a new ArrayList
of Item
elements from an input array.从输入数组创建一个新的
Item
元素的ArrayList
。 For two-dimensionals.对于二维。 In this case, you tried this:
在这种情况下,您尝试了以下操作:
public DenseBoard(T[][] x, T fillElem){
ArrayList<ArrayList<T>> myBoard = new ArrayList<ArrayList<T>>();
for(int i = 0; i < x.length; i++){
for(int j = 0; j < x[0].length; j++){
myBoard.get(i).add(j); //<<------ getting error!
}
}
}
The problem is that the line throwing the error assumes you have an ArrayList
of ArrayList
, but the inner element you try to refer to is not initialized.问题是抛出错误的行假设您有一个
ArrayList
的ArrayList
,但您尝试引用的内部元素未初始化。 This should be a fix:这应该是一个修复:
public DenseBoard(T[][] x, T fillElem){
ArrayList<ArrayList<T>> myBoard = new ArrayList<ArrayList<T>>();
for(int i = 0; i < x.length; i++){
myBoard.add(new ArrayList<T>(Arrays.asList(x[i])));
}
//Do something with myBoard
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.