merge two arraylist lists in list1 while it remain sorted

后端 未结 7 885
忘了有多久
忘了有多久 2021-01-16 11:00

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:23
         ArrayList<Integer> a = new ArrayList();
         ArrayList<Integer> b = new ArrayList();
         ArrayList<Integer> c = new ArrayList();
    
         a.add(1);
         a.add(3);
         a.add(5);
         a.add(7);
         a.add(17);
         a.add(27);
         a.add(37);
    
         b.add(0); 
         b.add(2);
         b.add(4);
    
         while( a.size() > 0 || b.size() >0){
    
            if( a.size() == 0 || b.size() == 0){
                c.addAll(b);
                c.addAll(a);
                break;
            }
    
            if(a.get(0) < b.get(0)){
                c.add(a.get(0));
                a.remove(0);
            }
            else{
                c.add(b.get(0));
                b.remove(0);
            }
    
        }
    
        System.out.println(c.toString());
    
    0 讨论(0)
  • 2021-01-16 11:25

    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<Integer> list1, ArrayList<Integer> list2)
     {
        list1.add(list2); //merges list 2 to list1
        Collections.sort(list1); //natural ordering
     }
    
    0 讨论(0)
  • 2021-01-16 11:27

    Easy fix: sort afterwards.

    list1.addAll(list2);
    Collections.sort(list1);
    

    Use sets to avoid duplicates.

    0 讨论(0)
  • 2021-01-16 11:34
    public static void merge (ArrayList<Integer> list1, ArrayList<Integer> list2)
    {
        list1.addAll(list2);
        Collections.sort(list1);
    }
    
    0 讨论(0)
  • 2021-01-16 11:34

    If your input Lists aren't too long, I'd suggest just merging the lists and using the Collections.sort() Method to restore the order:

    public static void mergeAndSort(List<Integer> list1, List<Integer> list2) {
        List<Integer> combinedList = new ArrayList<Integer>(list1);
        combinedList.addAll(list2);
        Collections.sort(combinedList);
        return combinedList;
    }
    

    As a sidenote, you should use the List interface instead of the ArrayList implementation class wherever possible.

    0 讨论(0)
  • 2021-01-16 11:35
    public list mergeAndSort(List<integer> list1, List<integer> list2){
    List<integer> list3;
    int list2Size = list2.size();
    for(int i=0;i<list2Size;i++){
        list1.add(list2(i));  
    }
    // Here we got all the elements in 1 list i.e list1
    
    int list1Size = list1.size();
    for(i=0;i<list1Size;i++){
        int small = 0;
        for(int j=i;j<list1size;j++){
            if(list1(i)> list2(j)){
                small = list2(j;
            }
        }
        list3.add(small); //Smallest 1 will be added to the new list
    }
    

    }

    0 讨论(0)
提交回复
热议问题