Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
排序a";“链接”;java中的列表_Java_Sorting - Fatal编程技术网

排序a";“链接”;java中的列表

排序a";“链接”;java中的列表,java,sorting,Java,Sorting,我在java中有以下实体: public class TodoEntity { private int id; private int previousId; } //Array to sort List<TodoEntity> unsorted=new ArrayList<>(); 今天的公共课{ 私有int-id; private int previousId; } //要排序的数组 List unsorted=new ArrayList(); 对数组

我在java中有以下实体:

public class TodoEntity {
   private int id;
  private int previousId;
}

//Array to sort
List<TodoEntity> unsorted=new ArrayList<>();
今天的公共课{
私有int-id;
private int previousId;
}
//要排序的数组
List unsorted=new ArrayList();
对数组进行排序的最快方法是什么,以使每个以前的id都与该id(分别是前/后的项目)匹配


当前实施

 public static List<TodoEntity> sortEntites(LinkedList<TodoEntity> todoEntities) {
    boolean isClean=true;
    while(isClean){
        boolean isRoundUnclean=false;
        for(int i=0;i<todoEntities.size();i++){
            TodoEntity todoEntity=todoEntities.get(i);
            if(i>0){
                TodoEntity todoEntityPrevious=todoEntities.get(i-1);
                if(todoEntity.getPrevious()!= null && !todoEntity.getPrevious().equals(todoEntityPrevious.getGuid())){
                    int index=getIndex(todoEntities,todoEntity.getPrevious());
                    todoEntities.remove(i);
                    todoEntities.add(index,todoEntity);
                    isRoundUnclean=true;
                    break;
                }
                if(todoEntity.getPrevious()==null){
                    todoEntities.remove(i);
                    todoEntities.add(0,todoEntity);
                }
            }
        }
        isClean = isRoundUnclean;
    }
    return todoEntities;
}
公共静态列表分类实体(LinkedList todoEntities){
布尔值isClean=true;
while(isClean){
布尔isRoundUnclean=false;
对于(int i=0;i0){
TodoEntity todoEntityPrevious=todoEntities.get(i-1);
如果(todoEntity.getPrevious()!=null&&!todoEntity.getPrevious().equals(todoEntityPrevious.getGuid())){
int index=getIndex(todoEntity,todoEntity.getPrevious());
删除(i);
添加(索引,todoEntity);
IsroundUnclen=正确;
打破
}
if(todoEntity.getPrevious()==null){
删除(i);
todoEntity.add(0,todoEntity);
}
}
}
isClean=不洁净;
}
回归实体;
}

此列表未链接。链表是存储引用和数据而不是数组数据的列表。要创建链接列表,只需更改

List unsorted=new ArrayList()


第一个呢?你所说的每个previousId与id匹配的
是什么意思?意思是->
todoEntity.get(i).previousId===todoEntity.get(i-1).id
如果你想让它们按顺序排列并且插入是一个频繁的操作,我不建议使用
ArrayList
,因为插入成本很高。我只需从您的
TodoEntity
集合中创建一个“真实的”
LinkedList
,通过迭代并将其添加到正确的位置听起来像是拓扑排序您不理解这个问题。这违反了接口的所有要求。
List<TodoEntity> unsorted = new LinkedList<>();
public class TodoEntity implements Comparable<TodoEntity>{
    private int id;
    private int previousId;
    @Override
    public int compareTo(TodoEntity other){
        if(this.id == other.previousID)
            return -1;
        if(this.previousID == other.id)
            return 1;
        return 0;
    }
}