我创建了自己的链表。我想使用Collections.sort方法对我的链表进行排序。 所以我将MyLinkedList类扩展为java.util.LinkedList。我还创建了Comparator和Comparable实现。但两者都不起作用。请在下面找到代码。
//链接列表实现。
package com.java.dsa;
class Node<E> {
E data;
Node<E> nextLink;
public Node(E data) {
this.data = data;
}
}
public class MyLinkedList<E> extends java.util.LinkedList<E>{
private static final long serialVersionUID = 1L;
private Node<E> firstNodePointer;
private Node<E> nodePointer;
public boolean isEmpty() {
return nodePointer == null;
}
public boolean add(E data) {
super.add(data);
Node<E> node = new Node<E>(data);
if (firstNodePointer == null) {
firstNodePointer = node;
nodePointer = node;
}else{
nodePointer.nextLink = node;
}
nodePointer = node;
return true;
}
public boolean remove(Object data){
super.remove(data);
Node<E> counterNodePointer = firstNodePointer;
Node<E> tempNodePointer = firstNodePointer;
while (counterNodePointer != null && !counterNodePointer.data.equals(data)) {
tempNodePointer = counterNodePointer;
counterNodePointer = counterNodePointer.nextLink;
}
if(tempNodePointer.equals(firstNodePointer)){
firstNodePointer = firstNodePointer.nextLink;
return true;
}
else if(counterNodePointer != null && tempNodePointer != null){
tempNodePointer.nextLink = counterNodePointer.nextLink;
return true;
}
return false;
}
public void printList() {
Node<E> counterNodePointer = firstNodePointer;
while (counterNodePointer != null) {
System.out.println(counterNodePointer.data);
counterNodePointer = counterNodePointer.nextLink;
}
}
}
//测试链接列表
package com.java.dsa;
import java.util.Collections;
import java.util.Comparator;
//员工类
class Employee implements Comparable<Employee> {
private String name;
private int id;
public Employee(String name, int id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
@Override
public String toString() {
return this.name + " " + this.id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Employee employee) {
return this.id - employee.id;
}
}
class EmployeeSort implements Comparator<Employee> {
@Override
public int compare(Employee emp1, Employee emp2) {
if (emp2.getId() - emp1.getId() > 0)
return 1;
else
return -1;
}
}
public class TestLinkedList {
public static void main(String[] args) {
MyLinkedList<Employee> myList = new MyLinkedList<Employee>();
for (int i = 10; i > 0; i--) {
Employee emp = new Employee("Sohan "+i, i);
myList.add(emp);
}
myList.printList();
Collections.sort(myList, new EmployeeSort());
myList.printList();
}
}
答案 0 :(得分:0)
java.util.LinkedList
不是为子类设计的类,你的代码可能只是破坏了它的内部和不变量。
如果您想自己实现链接列表,但希望自己节省实现完整List
接口的工作量,那么请使用AbstractList
作为基类。这门课的明确目的正是你想要做的。
答案 1 :(得分:0)
实际上它有效。只是你的内部数据结构没有被Collections.sort()
更新,并且由于你的断言使程序不能在printList()
的输出上工作,并且这依赖于该数据结构,你会看到元素的顺序未触及。请改用此方法:
public void printParentDataStructure() {
for ( E e : this ) System.out.println( e );
}
并且看到你的比较器完美地完成了它的工作。所以你的问题是你有两个数据结构,并没有让它们保持同步。你的下一个问题可能是“我怎样才能让它们保持同步?” - 好吧,基本上你应该覆盖每一个方法,并像{{1}一样调用super()
}和add()
。 不要这样做!这完全是胡说八道。
很明显,您希望实现用于学习数据结构的链表,但也许您应该首先更好地理解OOP编程的基本原理。