开发者

Elegant way of copying an ArrayList into two dimensional array

I am basically reading a text file into a List and I am copying the contents of the ArrayList into Object[][] like below. I need the to keep the return format Object[][] to use it in some other function. Is there any elegant way of doing this in Java? Thanks in advance.

public Object[][] getResults() {
  List<String> arrList =FileUtils.readLines(new File("myfile.txt"));   
  Object[][] result=new Object[arrList.size()][];       
  //how do I optimize this code below?
  int i=0;
  for(String s:arrList){
     result[i]=new开发者_运维问答 Object[]{new String(s)};
     i++;
  }
  return result;
}


You don't need to create a new String instance. Strings are immutable, and it's perfectly OK to share and reuse them.

int i = 0;
for (String s : arrList) {
    result[i] = new Object[] {s};
    i++;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜