merge two arraylist lists in list1 while it remain sorted

后端 未结 7 588
再見小時候
再見小時候 2021-01-16 10:43

In my assignment the third step is to Call the method merge to merge the two lists in list1 so that the list1 remains sorted.

I write my code but it

7条回答
  •  余生分开走
    2021-01-16 11:33

    After you merge the lists, call sort method as below.

    Collections.sort(list1); // carries out natural ordering.
    

    If you need a customized sorting, use Comparator object

    Collections.sort(list1, comparatorObject);
    

    Check Comparator example for more details.

    Here is your modified code:

     public static void merge (ArrayList list1, ArrayList list2)
     {
        list1.add(list2); //merges list 2 to list1
        Collections.sort(list1); //natural ordering
     }
    

提交回复
热议问题