简体   繁体   English

合并Java中的ArrayList排序

[英]Merge Sort with ArrayList in Java

I am trying to merge these ArrayLists. 我正在尝试合并这些ArrayList。 list3 is the final one. list3是最后一个。 Everything is working fine, but the last number ,400, wont show up in the final array. 一切正常,但最后一个数字400不会显示在最终数组中。 I have no clue why or what to do. 我不知道为什么或做什么。 I'm trying to get the ArrayList's sorted and I'm almost done except for that last number that wont show up. 我正在尝试对ArrayList进行排序,除最后一个不会显示的数字外,我几乎已经完成了。

import java.util.ArrayList;
public class TextLab12st
{
    public static void main(String args[])
{
    int jsaList1[] = {101, 105, 115, 125, 145, 165, 175, 185, 195, 225, 235, 275, 305, 315, 325, 335, 345, 355, 375, 385};
    int jsaList2[] = {110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 270, 280, 320, 350, 400};

    Array list1 = new Array(jsaList1,"List #1");
    Array list2 = new Array(jsaList2,"List #2");
    Array list3 = new Array("Merged List");

    list3.merge(list1,list2,list3);

    list1.display();
    list2.display();
    list3.display();

}

}

class Array
{
private ArrayList<Integer> list;
private int size;
private String listName;

public Array(String ln)
{
    list = new ArrayList<Integer>();
    size = 0;
    listName = ln;
}

public Array(int[] jsArray, String ln)
{
    list = new ArrayList<Integer>();
    size = jsArray.length;
    listName = ln;
    for (int j = 0; j < size; j++)
        list.add( new Integer( jsArray[j] ));
}

public void display ()
{
    System.out.println("\n" + listName + ":\n");
    System.out.println(list + "\n");
}

public void merge(Array that, Array theOther, Array result)
{
    {
        // Merge both halves into the result array
        // Next element to consider in the first array
        int iFirst = 0;
        // Next element to consider in the second array
        int iSecond = 0;

        // Next open position in the result
        int j = 0;
        // As long as neither iFirst nor iSecond is past the end, move the
        // smaller element into the result.
        while (iFirst < that.size && iSecond < theOther.size) 
        {
            if (that.list.get(iFirst) < theOther.list.get(iSecond)) 
            {
                result.list.add(that.list.get(iFirst));
                iFirst++;
            } 
            else 
            {
                result.list.add(theOther.list.get(iSecond));
                iSecond++;
            }
            j++;
        }

    }

}

} }

after the loop that runs over both arrays: 在两个数组上运行的循环之后:

    while (iFirst < that.size && iSecond < theOther.size) 
    {
        if (that.list.get(iFirst) < theOther.list.get(iSecond)) 
        {
            result.list.add(that.list.get(iFirst));
            iFirst++;
        } 
        else 
        {
            result.list.add(theOther.list.get(iSecond));
            iSecond++;
        }
        j++;
    }

you need to see if there's anything remaining in one of the arrays after you've reached the end of the other: 在到达另一个数组的末尾后,您需要查看其中一个数组是否还剩余什么:

if (iFirst < that,size) {
   //copy everything remaining in that to output
} else if (iSecond < theOther.size) {
   //copy everything from theOther to output
}
// this condition stops when you reach the end of either list
// you need to continue until you reach the end of both lists
 while (iFirst < that.size && iSecond < theOther.size)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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