简体   繁体   English

如何将元素从`2d array`复制到`Arraylist`?

[英]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 的每个成员:

Untested Code Ahead未经测试的代码

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));
}
  1. Try to use diamond operator <> to make code more readable尝试使用diamond operator <>使代码更具可读性
  2. Don't reinvent the wheel, use utilities provided at least by core libraries不要重新发明轮子,使用至少由核心库提供的实用程序
  3. Make use of for-each statement where you could尽可能使用for-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.问题是抛出错误的行假设您有一个ArrayListArrayList ,但您尝试引用的内部元素未初始化。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM