简体   繁体   English

使用Java插入双链双端列表时遇到问题

[英]Having trouble inserting into a doubly linked, double ended list with Java

public Link insert(String data, int where)
{
    Link temp = first;
    //If list is empty, insert as first
    if (first == null)
    {
        first = new Link(data, where);
        return first;
    }
    //List is not empty
    else
    {
        while (temp != null)
        {
            //If data is less than first, then insert as first
            if (data.compareTo(temp.getdData()) < 0 && temp == first)
            {
                Link newLink = new Link(data, where);
                newLink.setNext(first);
                first.setPrevious(newLink);
                first = newLink;
                return newLink;
            }
            //If data is less than current, then insert before current
            else if (data.compareTo(temp.getdData()) < 0)
            {
                Link newLink = new Link(data, where);
                newLink.setNext(temp);
                newLink.setPrevious(temp.getPrevious());
                temp.getPrevious().setNext(newLink);
                temp.setPrevious(newLink);
                return newLink;
            }
            //If data is more, but your at the end of the list.
            else if (data.compareTo(temp.getdData()) > 0 && temp.getNext() == null)
            {
                Link newLink = new Link(data, where);
                temp.setNext(newLink);
                newLink.setPrevious(temp);
                newLink.setNext(null);
                last = newLink;
                return newLink;
            }
            //Else keep going forward
            else
                temp = temp.next;
        }
    }
    return temp;
}

I can insert into the list, but for some reason it won't insert in order. 我可以插入列表,但是由于某种原因,它不会按顺序插入。 For example, if a link's data is "Hello" and I want to insert a link with "aaa", it won't insert the new link before the "Hello" link. 例如,如果链接的数据为“ Hello”,而我想插入“ aaa”链接,则不会在“ Hello”链接之前插入新链接。

Take a look at the way java compare string: 看看java比较字符串的方式:

java> "aaa".compareTo("Hello")    // positive number
java> "Aaa".compareTo("Hello")    // negative number

The reason is that ASCII code of character 'a' is larger than 'H'. 原因是字符“ a”的ASCII码大于“ H”。

BTW, you'd better use .equals() rather than == in your code to compare String in Java, take a look at this link: http://perso.ensta-paristech.fr/~diam/java/online/notes-java/data/expressions/22compareobjects.html 顺便说一句,您最好在代码中使用.equals()而不是==来比较Java中的String,请查看以下链接: http : //perso.ensta-paristech.fr/~diam/java/online/ notes-java / data / expressions / 22compareobjects.html

If you re-implement collections - it is the red flag you are doing something wrong. 如果您重新实现集合-这是危险信号,那是您做错了什么。

Your task can be solved much simpler: 您的任务可以更简单地解决:

public class Link {
    private final String data;
    private final int where;
    ...
}

public class LinkComparator implements Comparator<Link> {
    @Override
    public int compare(Link o1, Link o2) {
        return o1.data.compareToIgnoreCase(o2.data);
    }
}

public static void main(String[] args) {
    NavigableSet<Link> links = new TreeSet<Link>(new LinkComparator());

    links.add(new Link(...));
}

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

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