我需要一些关于排序节点的解释

我很困惑这个比较是如何工作的,例如第二个 if 语句,如果比较 > 0 它应该在 currentItem 之前,但是 if 语句内的行让我太困惑了,你能给我解释一下它们是如何工作的吗?

顺便一提

this.root

是 ListItem 的一个实例,代码不使用 LinkedList 或 ArrayList 类,下面的代码尝试模拟 LinkedList 中的某些内容,我认为,如果我的问题需要更多解释,请告诉我。此致

每次我运行我的应用程序时,它都会在标题中显示此错误,并且我已经搜索了一些问题,有人说将 ViewModel 构造函数公开,而我的是公开的,其他人则说:


从 HomeViewModel 中删除 Context 上下文和 LifecycleOwnerlifecycleOwner 构造函数参数,或者


创建一个可以构建 HomeViewModel 实例的 ViewModelProvider.Factory ,并将该工厂与 ViewModelProviders.of() 一起使用


我已经提出了两种解决方案,但仍然遇到相同的错误


主要活动


package com.example.architectureexample;


import androidx.appcompat.app.AppCompatActivity;

import androidx.lifecycle.Observer;

import androidx.lifecycle.ViewModelProvider;

import androidx.lifecycle.ViewModelProviders;


import android.os.Bundle;

import android.widget.Toast;


import java.util.List;


public class MainActivity extends AppCompatActivity {

//    5th video

    private NoteViewModel noteViewModel;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);

        noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {

            @Override

            public void onChanged(List<Note> notes) {

//                update recycleView

                Toast.makeText(MainActivity.this, "onChanged", Toast.LENGTH_SHORT).show();

            }

        });

    }

}


ibeautiful
浏览 114回答 1
1回答

POPMUISE

我添加了评论,以及一些修复:public boolean addItem(ListItem newItem) {&nbsp; &nbsp; if(this.root == null){&nbsp; &nbsp; &nbsp; &nbsp; this.root = newItem;&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; ListItem currentItem = this.root;&nbsp; &nbsp; while (currentItem != null){&nbsp; &nbsp; &nbsp; &nbsp; int comparison = currentItem.compareTo(newItem);&nbsp; &nbsp; &nbsp; &nbsp; if (comparison < 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// if cur < new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newItem.setPrevious(currentItem.previous());&nbsp; &nbsp;//&nbsp; &nbsp;advance cur&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (currentItem.next()!=null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentItem = currentItem.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//&nbsp; &nbsp;if end list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentItem.setNext(newItem);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp;append new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newItem.setPrevious(currentItem);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else if (comparison > 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // new < cur, insert before, fixes made to this part&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newItem.setNext(currentItem);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set&nbsp; &nbsp;new.nxt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newItem.setPrevious(currentItem.previous());&nbsp; &nbsp;// set&nbsp; &nbsp;new.prv&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (newItem.previous()!= null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if&nbsp; &nbsp; new.prv != 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newItem.previous().setNext(newItem);&nbsp; &nbsp; &nbsp; &nbsp;//&nbsp; &nbsp;set new.prv.nxt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.root = newItem;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//&nbsp; &nbsp;set root&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentItem.setPrevious(newItem);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set&nbsp; &nbsp;cur.prv&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp;// return false if duplicate&nbsp; &nbsp; }}使用文本图形。假设新节点 N 将插入到 A 之后和 B 之前。初始状态,cur (currentItem) = B:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cur&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;== B&nbsp; &nbsp; A <------ B&nbsp; &nbsp; 0 <- N&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cur.prv&nbsp; &nbsp; &nbsp;== A&nbsp; new.prv = 0&nbsp; &nbsp; A ------> B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;N -> 0&nbsp; &nbsp; cur.prv.nxt == B&nbsp; new.nxt = 0顺序是:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;N -> B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new.nxt&nbsp; &nbsp; &nbsp; = cur&nbsp; &nbsp; A <- N&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new.prv&nbsp; &nbsp; &nbsp; = cur.prv&nbsp; &nbsp; A -> N&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new.prv.nxt&nbsp; = new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;N <- B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cur.prv&nbsp; &nbsp; &nbsp; = new导致&nbsp; &nbsp; A -> N -> B&nbsp; &nbsp; A <- N <- B
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java