简体   繁体   English

删除和排序 LinkedList 中的项目 - Java

[英]Removing and sorting items in a LinkedList - Java

I have a LinkedList data structure I am using to store some items.我有一个 LinkedList 数据结构,用于存储一些项目。 This question is part of a course which requires me to use the standard java.utils LinkedList.这个问题是要求我使用标准 java.utils LinkedList 的课程的一部分。 The basic scenario is to be able to add in Books and Dictionaries to a LinkedList and then remove them.基本场景是能够将 Books 和 Dictionaries 添加到 LinkedList 然后删除它们。 The question I am stuck on now requires sorting the LinkedList so that all the Dictionaries are at the end of the LinkedList and the Books are at the start.我现在遇到的问题需要对 LinkedList 进行排序,以便所有词典都在 LinkedList 的末尾,而 Books 则在开头。

Here is my current code for the sorting of items:这是我当前用于项目排序的代码:

public static void moveDictionaries(Bookshelf b){
    int size = b.bookshelf.size();
    for(int i = 0; i<b.size(); i++) {
        Book temp = b.bookshelf.get(i);
        if(temp instanceof Dictionary){
            b.bookshelf.remove(temp);
            b.bookshelf.add(size - 1, temp);
        }
    }
}

A Dictionary is a subclass of Book. Dictionary 是 Book 的子类。 What I do is use a for loop to iterate through the list.我所做的是使用 for 循环来遍历列表。 I check whether the current item is a Dictionary, and if so, I add it to the end of the LinkedList, using the size as the position to add it into.我检查当前项目是否是一个字典,如果是,我将它添加到 LinkedList 的末尾,使用大​​小作为将其添加到的位置。 I then remove the item from the LinkedList.然后我从 LinkedList 中删除该项目。 Here is my Bookshelf class:这是我的书架类:

public class Bookshelf {

    List<Book> bookshelf = new LinkedList<Book>();

    public int size(){
        return bookshelf.size();
    }

    public void addBookOnLeftSide(Book b){
        bookshelf.add(b);
    }

    public void addBook(int i, Book b){
        bookshelf.add(i, b);
    }

    public Book remove(int i){
        Book temp = bookshelf.get(i);
        bookshelf.remove(i);
        return temp;
    }

    public void printLR(){
        for(int i = 0; i < bookshelf.size(); i++){
            System.out.println(bookshelf.get(i).toString());
        }
    }

    public void printRL(){
        for(int i = bookshelf.size() - 1; i>=0; i--){
            System.out.println(bookshelf.get(i).toString());
        }
    }
}

And lastly my Main class where i am testing it:最后是我正在测试它的 Main 类:

 public static void main(String[] args) {

        Book bookOne = new Book(101, "Hello");
        Book bookTwo = new Book(102, "Bye");
        Book bookThree = new Book(103, "Morning");
        Dictionary dictionaryOne = new Dictionary("104", "Afternoon", 10, "English", "Spanish", 10);
        Dictionary dictionaryTwo = new Dictionary("105", "Evening", 10, "English", "Spanish", 10);

        Bookshelf bookshelf = new Bookshelf();

        bookshelf.addBookOnLeftSide(dictionaryOne);
        bookshelf.addBookOnLeftSide(bookOne);
        bookshelf.addBookOnLeftSide(bookThree);
        bookshelf.addBookOnLeftSide(dictionaryTwo);
        bookshelf.addBookOnLeftSide(bookTwo);

        moveDictionaries(bookshelf);
        bookshelf.printLR();
    }

The problem I am having is that when I run this method, it does not work as expected.我遇到的问题是,当我运行此方法时,它没有按预期工作。 It only moves one of the Dictionaries and the other one somehow goes missing.它只会移动一本词典,而另一本不知何故丢失了。 I am not sure why this is happening.我不确定为什么会这样。 Any help would be great.任何帮助都会很棒。

Consider your code:考虑你的代码:

for(int i = 0; i<b.size(); i++) {
    Book temp = b.bookshelf.get(i);
    if(temp instanceof Dictionary){
        b.bookshelf.remove(temp);
        b.bookshelf.add(size - 1, temp);
    }
}

Consider the cases where you have a bookshelf: B1, D1, D2, B3 and i = 1考虑您有书架的情况:B1、D1、D2、B3 和 i = 1

In this situation D1 will be moved to the end and i will be incremented leaving a bookshelf of B1, D2, B3, D1 and i = 2在这种情况下,D1 将移到末尾,并且 i 将递增,留下 B1、D2、B3、D1 和 i = 2 的书架

On the next iteration B3 will be considered, skipping D2 altogether.在下一次迭代中将考虑 B3,完全跳过 D2。

Your code should look something like你的代码应该看起来像

int start = 0;
int end = bookshelf.size();
while (start < end) {
    if (bookshelf.get(start) instanceof Dictionary) {
        bookshelf.moveToEnd(start);
        end--;
    } else {
        start++;
    }
}

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

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