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++;
}
精彩评论