我正在使用嵌套的Ordered ListNode类实现自定义Ordered LinkedList类。一切都运行正常,但我试图通过访问被删除的元素来扩展它。这不是一个要求,但我很好奇这是如何工作的,因为我只能使用我被指示创建的方法,它们是boolean add(),boolean remove()和clear()。
我还跟踪每个修改,每次修改都会随着每次成功添加,删除或调用clear()而增加。我可以简单地创建另一个OrderedLinkedList,并将删除的元素添加到它,但我觉得我添加了不必要的修改计数。
有没有人有任何建议?同样,这部分只是为了好玩而不是必需的。我觉得这会让我对创建自定义类有更深入的了解。
我将展示删除和主要方法。删除方法签名无法更改。
public boolean remove(Comparable obj) {
for(OrderedListNode element = head.next; element != tail; element = element.next) {
if(obj.equals(element.dataItem)) { //if element being removed is at the cursor
OrderedListNode previousNode = element.before;
OrderedListNode nextNode = element.next;
nextNode.before = previousNode; //places next element that's after before to the element after current element [prev -> current -> next]
previousNode.next = nextNode; //places prev of next element to the element before current
element.dataItem = (Comparable)NOT_FOUND; //removed element is now null
modCount++; //another modification
theSize--; //reduce the size by 1
return true; //if remove is successful
}
}
return false; //otherwise, not successful removal
}
主要方法:
public static void main(String[] args) {
OrderedLinkedList list = new OrderedLinkedList();
OrderedLinkedList removedList = new OrderedLinkedList();
modCount = 0;
list.add("Dog");
list.add("Bird");
list.add("dog");
list.add("bird");
list.add("Cat");
System.out.println("Before removal of element");
System.out.println(list);
list.remove("Dog");
removedList.add("Dog"); //not what I'm wanting to do
System.out.println("Removed " + removedList);
System.out.println("After removal of element");
System.out.println(list);
System.out.println("Total modifications = " + modCount);
System.out.println();
}
输出:
Before removal of element
Bird, Cat, Dog, bird, dog
Removed Dog //not actually accessing the element originally removed. just printing a new list
After removal of element
Bird, Cat, bird, dog
Total modifications = 7 //unnecessary modification due to additional add
答案 0 :(得分:2)
如果您只想存储已删除的元素而不增加修改计数,则可以使用ArrayList并将删除的元素放入其中。这样您的修改计数就不会受到影响。
答案 1 :(得分:1)
您可以通过实施其他 pop 方法来存储已删除的值。返回类型应为 Comparable ,当找到要删除的对象时,将其存储在临时对象中并返回该对象,而不是返回布尔值 true 。找不到对象时,只需返回 null 。
如果找到要删除的 Comparable 对象,该方法将返回该对象,以便您可以存储它。如果没有,则会返回null,以便您可以使用检查 pop 方法获取,如果删除成功与否。
以下是我刚刚为您编写的示例方法;
public Comparable pop(Comparable obj) {
for (OrderedListNode element = head.next; element != tail; element = element.next) {
Comparable temp = null; // declaration of the temporary object
if (obj.equals(element.dataItem)) { // if element being removed is
// at the cursor
temp = obj; // store obj in temp
OrderedListNode previousNode = element.before;
OrderedListNode nextNode = element.next;
nextNode.before = previousNode; // places next element that's
// after before to the element
// after current element [prev
// -> current -> next]
previousNode.next = nextNode; // places prev of next element to
// the element before current
element.dataItem = (Comparable) NOT_FOUND; // removed element is
// now null
modCount++; // another modification
theSize--; // reduce the size by 1
return temp; // if remove is successful
}
}
return null; // otherwise, not successful removal
}
您的测试代码应该是这样的;
public static void main(String[] args) {
OrderedLinkedList list = new OrderedLinkedList();
OrderedLinkedList removedList = new OrderedLinkedList();
modCount = 0;
list.add("Dog");
list.add("Bird");
list.add("dog");
list.add("bird");
list.add("Cat");
System.out.println("Before removal of element");
System.out.println(list);
// list.remove("Dog"); // not needed anymore
// removedList.add("Dog"); //not what I'm wanting to do
// pop returns the removed object
removedList.add(list.pop("Dog"));
System.out.println("Removed " + removedList);
System.out.println("After removal of element");
System.out.println(list);
System.out.println("Total modifications = " + modCount);
System.out.println();
}