[英]LinkedList indexOf method check
i am currently building a linked list from scratch and everything seems to be going okay except on my test driver when i try and get an index of a certain object it gives me a value that seems to be 2 values off, (should be 3, its 5) 我目前正在从头开始构建一个链表,并且一切正常,除了在我的测试驱动程序上,当我尝试获取某个对象的索引时,它给我的值似乎减少了2个值,(应为3,其5)
Test Driver 测试驱动
public static void main ( )
{ List<String> friends = new LinkedList <String> ();
System.out.println (" Testing problem 1");
friends.add ("joe");
friends.add ("mary");
friends.add ("jim");
friends.add ("joe"); // Lists may contain duplicate elements
friends.add (2, "sally"); // Insert at position 2
System.out.println (friends.get(3)); // Should be jim
friends.remove (0);
System.out.println (friends.indexOf ("joe")); // Should be 3
String sal = new String ("sa" + "lly");
if (! friends.contains(sal)) // "sally"
System.err.println ("Not correct");
My indexOf method 我的indexOf方法
/**
*
* @returns index value, -1 if object was not found
*/
public int indexOf(Object obj) {
int index = 0;
Node<E> current = head.next;
while (current != null) {
if (current.equals( obj)) {
return index;
}
index++;
current = current.next;
}
if(index == size && obj == null){
return -1;
}
else{
return index;
}
}
My add(E value) method 我的add(E value)方法
public void add(E value){
Node<E> temp = new Node<E>(value,tail,tail.previous);
tail.previous = temp;
temp.previous.next = temp;
size++;
}
My add(int index, E value) method 我的add(int index,E value)方法
public void add(int index, E value) {
setRef(index);
Node<E> temp = new Node<E>(value,ref,ref.previous);
temp.previous.next = temp;
ref.previous = temp;
size++;
}
any idea why i may be getting an index of 5 when it should be 3? 任何想法,为什么我可能会得到5时应该是3的索引?
For those who want full source 对于那些想要完整资料的人
LinkedList.java LinkedList.java
package list;
public class LinkedList<E> implements List<E>
{
//size of list
int size = 0;
Node<E> head;
Node<E> tail;
Node<E>ref;
public LinkedList(){
head = new Node<E>(null,null,null);
tail = new Node<E>(null,null,head);
head.next = tail;
}
public void add(E value){
Node<E> temp = new Node<E>(value,tail,tail.previous);
tail.previous = temp;
temp.previous.next = temp;
size++;
}
public E get(int index){
setRef(index);
return ref.value;
}
public E set(int index, E value){
setRef(index);
E result = ref.value;
ref.value = value;
return result;
}
public void add(int index, E value) {
setRef(index);
Node<E> temp = new Node<E>(value,ref,ref.previous);
temp.previous.next = temp;
ref.previous = temp;
size++;
}
public int size(){
return size;
}
public E remove(int index){
setRef(index);
E result = ref.next.value;
ref.previous.next = ref.next;
ref.next.previous = ref.previous;
size --;
return result;
}
public void clear(){
size = 0;
head.next = tail;
tail.previous = head;
ref = null;
}
/**
* @returns true if size of list is equal to 0
*/
public boolean isEmpty(){
if(size == 0 ){
return true;
}
else{
return false;
}
}
private void setRef(int index){
ref = head.next;
for(int i = 0; i < index; i++){
ref = ref.next;
}
}
/**
*
* @returns index value, -1 if object was not found
*/
public int indexOf(Object obj) {
int index = 0;
Node<E> current = head.next;
while (current != null) {
if (current.equals( obj)) {
return index;
}
index++;
current = current.next;
}
if(index == size && obj == null){
return -1;
}
else{
return index;
}
}
/**
* @returns true if list contains Object obj
*/
public boolean contains(Object obj){
boolean isTrue;
if(!(this.indexOf(obj) == -1)){
isTrue = true;
}
else{
isTrue = false;
}
return isTrue;
}
public String toString() {
String result = "[";
Node<E> current = head;
while(current.next != null){
current = current.next;
result += current.value + ", ";
}
return result += "]";
}
public RefIterator<E> Riterator(){
return new RefIterator<E>(this);
}
public ArrayIterator<E>iterator(){
return new ArrayIterator<E>(this);
}
public ListIterator<E> listIterator(){
return new RefListIterator<E>(this);
}
public ListIterator<E> listIterator(int start){
return new RefListIterator<E>(this,start);
}
}
List.java List.java
public interface List<E>
{
/**
* @return the size of this list
*/
int size(); // all methods in interfaces must be public
/**
* Clear this list
*/
void clear();
/**
*@return the value at a given position by index
*/
E get(int index); // index must be in bounds -- check for that later
/**
* @returns the value that is being replaced
*/
E set(int index, E value);
/**
* Insert a given value at the position index
*/
void add(int index, E value);
/**
* Inserts a given value at the end of index
*/
void add(E value);
/**
* @returns true if List is empty
*/
boolean isEmpty();
/**
* removes value at the given index
* @return value being removed
*/
E remove(int index);
int indexOf(Object obj);
boolean contains(Object obj);
String toString();
/**
* @return refrence to the iterator
*/
ArrayIterator<E>iterator();
RefIterator<E> Riterator();
ListIterator<E> listIterator();
ListIterator<E> listIterator(int start);
}
Test Driver 测试驱动
package list;
import list.*;
/**
* Lab 3
* Test methods added to the List interface
*
* @author (sdb)
* @version (Sep 2015)
*/
public class DriverLab01PM
{
/**
* This main method tests the List classes
* for lab 1, Data Structures and Algorithms
*/
public static void main ( )
{ List<String> friends = new LinkedList <String> ();
System.out.println (" Testing problem 1");
friends.add ("joe");
friends.add ("mary");
friends.add ("jim");
friends.add ("joe"); // Lists may contain duplicate elements
friends.add (2, "sally"); // Insert at position 2
System.out.println (friends.get(3)); // Should be jim
friends.remove (0);
System.out.println (friends.indexOf ("joe")); // Should be 3
String sal = new String ("sa" + "lly");
if (! friends.contains(sal)) // "sally"
System.err.println ("Not correct");
//////////// Uncomment the following when ready for problem 2
System.out.println ("\n\n Testing problem 2");
System.out.println (friends); // [mary, sally, jim joe]
List <String> enemies = null;
System.out.println (enemies);
enemies = new ArrayList<String> ();
System.out.println (enemies);
enemies.add ("mike");
enemies.add ("mick");
System.out.println (enemies);
if (! enemies.contains ("mike"))
System.err.println ("Not correct");
if (enemies.contains ("Mike"))
System.err.println ("Not correct");
// //////////// Uncomment the following when ready for problem 3
System.out.println ("\n\n Testing problem 3");
WordList wordList = new WordList();
List <String> words = wordList.getWordList();
System.out.println (words.indexOf ("jack")); // Should be 51595
if (!words.contains ("zoo"))
System.err.println ("Error in contains");
if (words.contains ("foobar"))
System.err.println ("Error in contains");
wordList = new WordList();
List <String> moreWords = wordList.getWordList();
if (!words.equals (moreWords))
System.err.println ("Error in equals");
if (!moreWords.equals (words))
System.err.println ("Error in equals");
moreWords.add (0, "foobar");
if (words.equals (moreWords))
System.err.println ("Error in equals");
if (moreWords.equals (words))
System.err.println ("Error in equals");
moreWords.remove(0);
moreWords.add ("foobar");
if (words.equals (moreWords))
System.err.println ("Error in equals");
if (moreWords.equals (words))
System.err.println ("Error in equals");
String foobar = new String ("foo" + "bar");
words.add (foobar); // "foobar"
if (!words.equals (moreWords))
System.err.println ("Error in equals");
if (!moreWords.equals (words))
System.err.println ("Error in equals");
System.out.println ("Testing complete");
}
}
Node class 节点类
package list;
public class Node<E>
{
E value;
Node<E> next;
Node<E> previous;
Node(E tempValue, Node<E> tempNext, Node<E> tempPrev){
this.value = tempValue;
this.next = tempNext;
this.previous = tempPrev;
}
}
Without checking the logic of your other code, your indexOf method should look like this: 在不检查其他代码的逻辑的情况下,indexOf方法应如下所示:
public int indexOf(Object obj) {
int index = 0;
Node<E> current = head;
while (current != null) {
if (current.equals(obj)) {
return index;
}
index++;
current = current.next;
}
return -1;
}
Basically, if you haven't returned a value from the body of the for
loop, then you can deduce that the list does not contain the target value, and correctly return -1
. 基本上,如果您尚未从for
循环的主体中返回任何值,则可以推论该列表不包含目标值,并正确返回-1
。 This is how you need to override your equals
method 这就是您需要重写equals
方法的方式
public class Node<E> {
//...
public boolean equals(Object o){
return value.equals(o);
}
//...
}
This works, because <E>
is functionally the same as <E extends Object>
. 这是可行的,因为<E>
在功能上与<E extends Object>
。 And if your calling equals
of an integer type or whatever type your specifying (which is a subclass of object) at run-time it will call the equals
method residing in the Integer
class, or the closest overridden version of equals
when walking up the inheritance chain, not the Object
method 并且,如果您在运行时调用equals
整数类型或您指定的类型(它是对象的子类)的任何类型,它将调用驻留在Integer
类中的equals
方法,或者在继承继承时调用最接近的覆盖版本的equals
链,而不是Object
方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.