lundi 23 février 2015

Do I need to new ArrayList in loop every time?



Here, I created a new 2D ArrayList and sort it.



//1. sort list, based on col 0.
//list : List<List<Integer>>
List<ArrayList<Integer>> sortedList = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < list.size(); i++){
sortedList.add(new ArrayList<Integer>(list.get(i))); //===> question for this line!
}

Collections.sort(sortedList, new Comparator<ArrayList<Integer>>() {
@Override
public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
return o1.get(0).compareTo(o2.get(0));
}
});


I have a question, for my question line. (see arrow above), do I need to "new" it as I did, or I can just call



sortedList.add(list.get(i));



Aucun commentaire:

Enregistrer un commentaire