简体   繁体   English

在 Java 中使用按 ID 编号的链表升序

[英]Ascending order using a Linked List by ID number in Java

So I have a list of items, with random ID numbers assigned to them.所以我有一个项目列表,并为它们分配了随机 ID 号。 Those items then print out in a list.然后将这些项目打印在列表中。 I need to able to print them out in ascending order.我需要能够按升序打印出来。 The constructor in the LinkedList class should call a method to build a list of test data. LinkedList 类中的构造函数应该调用一个方法来构建测试数据列表。 These should be added to the LinkedList in sorted order.这些应该按排序顺序添加到 LinkedList 中。 Below is the code I need to edit to make this work:下面是我需要编辑的代码来完成这项工作:

public boolean decideToInsert (Makeup ee) {
boolean decision = false;
//code goes here
return decision;

Here is my linked list code:这是我的链表代码:

package model;
import java.util.LinkedList;
import java.util.ListIterator;

public class MakeupList {

private LinkedList <Makeup> makeList = new LinkedList<>();
public MakeupList(){
    this.createMakeList();
    this.printMakeList();
}
public void createMakeList(){
    makeList.add(new Makeup("Natural Face", "Too Face"));

    for (int i = 0; i<10; i++){
        addMakeup (new Makeup ("Matte Lipstick", "MAC"));
}
}

public void addMakeup (Makeup newMake){
    boolean makeupAdded = false;
    boolean insertDecision = false;
    ListIterator<Makeup> makeIterator = makeList.listIterator();
    while (makeIterator.hasNext()){
        insertDecision = makeIterator.next().decideToInsert(newMake);
        if (insertDecision == true){
            makeList.add(makeIterator.previousIndex(), newMake);
            makeupAdded = true;
            break;
        } 
    }
    if(!makeupAdded){
        makeList.add(newMake);
    }
}
public void printMakeList(){
    ListIterator<Makeup> makeIterator = makeList.listIterator();
    while(makeIterator.hasNext()){
        System.out.println(makeIterator.next().toString());
        }
    }
}

Here is an example of what it is currently printing out: Makeup: Natural Face Too Face ID: 1这是当前打印的示例:Makeup: Natural Face Too Face ID: 1

Makeup: Matte Lipstick MAC ID: 1彩妆:哑光唇膏 MAC ID:1

Makeup: Matte Lipstick MAC ID: 0彩妆:哑光唇膏 MAC ID:0

Makeup: Matte Lipstick MAC ID: 2彩妆:哑光唇膏 MAC ID:2

Makeup: Matte Lipstick MAC ID: 37彩妆:哑光唇膏 MAC ID:37

Makeup: Matte Lipstick MAC ID: 10彩妆:哑光唇膏 MAC ID:10

Makeup: Matte Lipstick MAC ID: 21彩妆:哑光唇膏 MAC ID:21

Makeup: Matte Lipstick MAC ID: 34彩妆:哑光唇膏 MAC ID:34

Makeup: Matte Lipstick MAC ID: 8彩妆:哑光唇膏 MAC ID:8

Makeup: Matte Lipstick MAC ID: 33彩妆:哑光唇膏 MAC ID:33

Makeup: Matte Lipstick MAC ID: 19彩妆:哑光唇膏 MAC ID:19

Your code is very complicated.您的代码非常复杂。 You just add an id into your Makeup model and do a java stream sorting by comparator.您只需将一个 id 添加到您的 Makeup 模型中,然后通过比较器进行 java 流排序。 More details are here: java 8, Sort list of objects by attribute without custom comparator更多细节在这里: java 8, Sort list of objects by attribute without custom comparator

Try using:尝试使用:

Collections.sort(linkedlistVAR);

before you print it out.在你打印出来之前。 :-) :-)

I don't quite understood your question.我不太明白你的问题。 If Makeup has an id atribute and you wanna sort by it try sorting with the Comparator interface on the start of you print method.如果 Makeup 有一个 id 属性,并且您想按它排序,请尝试在打印方法开始时使用 Comparator 接口进行排序。

Example: makeList.sort(Comparator.nullsFirst(Comparator.comparing(Makeup::getId, Comparator.nullsFirst(naturalOrder()))));示例: makeList.sort(Comparator.nullsFirst(Comparator.comparing(Makeup::getId, Comparator.nullsFirst(naturalOrder()))));

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

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