简体   繁体   中英

Sorting two arraylists into a new sorted list

I am working on a project so I've simplified my problem to this:

Given two arraylists that are sorted in decending order, I want to take each of their values and put them into a new arraylist.

The new arraylist has a size capacity of 5 maximum.

So this seems to be pretty easy maybe I am just having a brainfart but here is my code, I keep getting nullpointer exception and I have NO IDEA WHY

        int l1ptr = 0;
        int l2ptr = 0;
        while(searchList.size() < 5 || (listOne.get(l1ptr) == null && listTwo.get(l2ptr) == null)){
            if(listOne.get(l1ptr) == null){
                searchList.add(listTwo.get(l2ptr).document);
                l2ptr++;
            }else{
                if(listTwo.get(l2ptr) == null){
                    searchList.add(listOne.get(l1ptr).document);
                    l1ptr++;
                }else{
                    if(listOne.get(l1ptr).frequency < listTwo.get(l2ptr).frequency){
                        searchList.add(listTwo.get(l2ptr).document);
                        l2ptr++;
                    }else{
                        if(listTwo.get(l2ptr).frequency < listOne.get(l1ptr).frequency){
                            searchList.add(listOne.get(l1ptr).document);
                            l1ptr++;
                        }else{
                            searchList.add(listOne.get(l1ptr).document);
                            l1ptr++;
                        }
                    }
                }
            }

SearchList is the new arraylist I want to return.

The two lists given are lists of objects, which store both a string(document) and number(frequency).

Any help would be much appreciated.

If I understand correctly, you want to merge two sorted lists, up to maximum 5 elements. Consider this generalized logic:

  • Repeat while searchList.size() < 5 and l1ptr is not at the end of the first list and l2ptr is not at the end of the second list

    • The loop will stop when either you have 5 elements, or reached the end of one of the lists
  • Repeat while searchList.size() < 5 and l1ptr is not at the end of the first list

    • The loop will stop when either you have 5 elements, or reached the end of the first list
    • Note that if you have already reached the end of the first list in the first loop, then this loop will simply do nothing
  • Repeat while searchList.size() < 5 and l2ptr is not at the end of the second list

    • The loop will stop when either you have 5 elements, or reached the end of the second list
    • Note that if you have already reached the end of the second list in the first loop, then this loop will simply do nothing
  • At the end, you will have searchList with at most 5 elements, correctly ordered

Like this:

while (searchList.size() < 5 && l1ptr < listOne.size() && l2ptr < listTwo.size()) {
    if (listOne.get(l1ptr).frequency < listTwo.get(l2ptr).frequency) {
        searchList.add(listTwo.get(l2ptr).document);
        l2ptr++;
    } else {
        searchList.add(listOne.get(l1ptr).document);
        l1ptr++;
    }
}

while (searchList.size() < 5 && l1ptr < listOne.size()) {
    searchList.add(listOne.get(l1ptr).document);
    l1ptr++;
}

while (searchList.size() < 5 && l2ptr < listTwo.size()) {
    searchList.add(listTwo.get(l2ptr).document);
    l2ptr++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM