gpt4 book ai didi

java - 使用比较器和泛型 java 对链表进行排序

转载 作者:行者123 更新时间:2023-12-01 09:55:46 25 4
gpt4 key购买 nike

我正在尝试使用泛型对链接列表进行排序,但我遇到了一些转换问题。代码抛出 Bus can't be Casted to Node.js 错误。我知道问题出在比较器中(我在其中转换为 Bus),但否则我不知道如何调用 Bus 中定义的方法(与 Bus 或另一个对象无关,只是使用随机对象进行测试)。我一直在互联网上进行研究,但找不到解决方案。这是代码:

/**
* Swaps the current node's element with the previous one
*/
public void swap(){
Object previous = getCurrent().getElement();
Object current = next().getElement();
getCurrent().setElement(previous);
previous().setElement(current);
}

public AbstractList<T> orderBy(Comparator<Node<T>> comparator){
setCurrent(getFirst());
Node<T> aux;
Node<T> current;
boolean check = true;
while (check){
check = false;
aux = getFirst();
current = getFirst().getNext();
while(hasNext()) {
if (comparator.compare(aux, current) > 0) {
check = true;
swap();
}
aux = current;
current = current.getNext();
}
}
return this;
}

比较器:

import java.util.Comparator;
public class InternComparator implements Comparator<Node<Bus>>{

@Override
public int compare(Node<Bus> o1, Node<Bus> o2) {
return ((Bus)o1.getElement()).getIntern() - ((Bus)o2.getElement()).getIntern();
} //getIntern() returns an Integer

摘要列表(由教授提供):

    import java.util.NoSuchElementException;
public class AbstractList<T> {
private Node<T> first;
private Node<T> current;
private Node<T> last;
private int size = 0;


public boolean hasNext(){
return current != last;
}

public boolean hasPrevious(){
return current != first;
}

public Node getCurrent(){
return current;
}

public void setCurrent(Node<T> current) {
this.current = current;
}

public int size(){
return size;
}

public boolean isEmpty(){
return first == last || first == null;
}

public void add(T t){
Node<T> a = new Node<T>(t);
if(first == null){
first = a;
}
if(last == null && first != null){
last = a;
last.setPrevious(first);
first.setNext(last);
}

Node<T> aux = last;
last.setNext(a);
last = a;
last.setPrevious(aux);
current = last;
size++;
}

public Node next(){
if(!hasNext()){
throw new RuntimeException("No elements available next.");
}
current = current.getNext();
return current;
}

public Node previous(){
if(!hasPrevious()){
throw new RuntimeException("No elements available previous.");
}
current = current.getPrevious();
return current;
}

public Node getFirst(){
return first;
}

public Node getLast(){
return last;
}

public void setFirst(Node<T> first){
this.first = first;
}

public void setLast(Node<T> last) {
this.last = last;
}

public void remove(T t){
current = first;
boolean removed = false;
while (hasNext()) {

if (current.getElement() == t || current.getElement().equals(t)) {
if(current != last && current != first) {
current.getNext().setPrevious(current.getPrevious());
current.getPrevious().setNext(current.getNext());
} else if(current == first){
current.getNext().setPrevious(null);
} else if(current == last){
current.getPrevious().setNext(null);
}
removed = true;
return;
}

current = next();
}

if(removed){
size--;
} else {
throw new NoSuchElementException("No such element on the list.");
}
}

public Node goTo(int pos){
if(pos > size){
throw new ArrayIndexOutOfBoundsException("Inexistent Position");
}
current = first;
for(int i = 0; i < pos; i++){
current = next();
}
return current;
}

public void insertAfter(T t){
Node<T> aux = new Node<>(t);
if(current != last) {
Node<T> next = current.getNext();
Node<T> previous = current;
current.getNext().setPrevious(aux);
current.setNext(aux);
aux.setNext(next);
aux.setPrevious(previous);
} else {
current.setNext(aux);
aux.setPrevious(current);
last = aux;
}
size++;
}

public void insertPrevious(T t){
Node<T> aux = new Node<>(t);
if(current != first) {
Node<T> previous = current.getPrevious();
Node<T> next = current;
current.getPrevious().setNext(aux);
current.setPrevious(aux);
aux.setNext(next);
aux.setPrevious(previous);
} else {
Node<T> aux1 = current;
current.setPrevious(aux);
first = aux;
first.setNext(aux1);
}
size++;
}

@Override
public String toString() {
String result = "";
current = first;
while(hasNext()) {
result += current.getElement() + "";
next();
}
return result;
}
}

class Node<T> {
private Object element;
private Node<T> prev;
private Node<T> next;

public Node(T element){
this.element = element;
}

public Node(T element, Node next){
this.element = element;
this.next = next;
}

public Node<T> getNext() {
return next;
}

public Node<T> getPrevious() {
return prev;
}

public Object getElement() {
return element;
}

public void setNext(Node<T> next) {
this.next = next;
}

public void setPrevious(Node<T> prev) {
this.prev = prev;
}

public void setElement(Object element) {
this.element = element;
}
}

最佳答案

再想一想:你的对象的类型是Node<Bus> ;你想知道为什么要强制转换为 Bus失败?

或者让我们重新表述一下:你是否假设 Bus<People>代表一个人?

如果你有一个容器,那么你必须检索其中包含的值;而这不能通过类型转换容器来实现!

或者,继续使用奇怪的图片:你不能通过声明盒子是鸡蛋来从盒子里取出鸡蛋。你打开盒子,取出鸡蛋。

关于java - 使用比较器和泛型 java 对链表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37239396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com