链接列表中的对象问题

时间:2015-10-27 02:21:18

标签: java object

在我的count方法中,我试图将正在发送的对象与调用它的对象进行比较,我不知道为什么我无法调用o.get(i)来获取Object o的值被传递。它告诉我即使我可以正常调用get(i),也找不到方法。任何想法我如何获得对象的价值得到通过所以我可以比较它们?

public class GenericLinkedList<E> implements List<E> {

private class Node<E>{
    private E data;
    private Node<E> next;
}
private E data;
private Node<E> head = null;
private int size = 0;

private Node<E> nodeAt(int index){
    Node<E> curNode = head;
    int curIndex = 0;
    while(curIndex < index){
        curNode = curNode.next;
        curIndex++;
    }
    return curNode;
}

@Override
public E get(int index) {
    return nodeAt(index).data;
}

@Override
public void add(E value) {
    Node<E> node = new Node<E>();
    node.data = value;

    if(size == 0){
        head = node;
    }else{
        Node<E> curNode = nodeAt(size - 1);
        curNode.next = node;
    }
    size++;
}

public void add(int index, E value){
    Node<E> node = new Node<E>();
    node.data = value;

    if(size == 0){
        head = node;
    }else if(index == 0){
        node.next = head;
        head = node;
    }else{
        Node<E> curNode = nodeAt(index - 1);
        node.next = curNode.next;
        curNode.next = node;
    }
    size++;
}

@Override
public void remove(int index) {
    if(index == 0){
        head = head.next;
    }else{
        Node<E> curNode = nodeAt(index - 1);
        curNode.next = curNode.next.next;
    }
    size--;

}

@Override
public void set(int index, E value) {
    nodeAt(index).data = value;

}

public String toString(){
    String s = "";
    Node<E> curNode = head;
    s += curNode.data +" -> ";
    while(curNode.next != null){
        curNode = curNode.next;
        s += curNode.data +" -> ";
    }
    s += "null";
    return s;
}

public void clear() {     

    for (int i = 0; i < size; i++)
        set(i, null);

    size = 0;
}

@Override 
public void removeAll(E o)//Clears out the array object by setting everything to null
{        
    for(int i = 0; i < this.size; i++)
    {    

    }
}

@Override
public int count(Object o)
{
    System.out.println(o);
    int count = 0; 

    for(int i = 0; i< size;i++)
    {
        if(get(i) == o.get(i))
        {
            count++;
        }
    }
    return count;


    /*int count = 0;
    for(int i = 0;i<this.size;i++)
    {
    for(int j = 0;i<size;j++)
    {

    }
    }
    return count;*/

}

public void reverse()
{
    int x = size;
    for(int i = 0; i < this.size; i++)
    {
        E temp = this.get(x);
        this.set(x, get(i));
        this.set(i, temp);
        x++;        

    }

}

public Object subList(int beginIndex, int endIndex)
{
    int j = 0;
    GenericLinkedList<E> LinkedList = new GenericLinkedList<E>();
    if(beginIndex == endIndex)
        return LinkedList;
    else if(beginIndex > endIndex||endIndex>size||beginIndex<0)
    {
        return null;
    }
    else 
    {
        for(int i = beginIndex; i <= endIndex;i++)
        {              
            LinkedList.add(get(i));
            j++;
        }
    }        

    return LinkedList;

}

public static void main(String[] args) {
    GenericLinkedList<Integer> myList = new GenericLinkedList<Integer>();
    myList.add(4);
    myList.add(7);
    myList.add(123123);

    System.out.println(myList.get(2));

    GenericLinkedList<Integer> myList2 = new GenericLinkedList<Integer>();
    myList2.add(4);
    myList2.add(7);
    myList2.add(8);
    myList2.add(3,999);
    System.out.println(myList2);       

    System.out.println(myList.count(myList2));

    //System.out.println(myList2);
    // myList2.clear();//Testing our clear class     
    // System.out.println(myList2);  
    //System.out.println(myList2.subList(1, 3));//Testing our clear class     
    // System.out.println(myList2);  

}

}

2 个答案:

答案 0 :(得分:0)

尝试调用o.get()时遇到问题,因为它是基类型Object的对象,没有定义方法。为了得到正确的结果,你有两种可能的解决方案......

简短的解决方案是输入。

   ((GenericLinkedList<Object>)o).get(i)

或者更容易阅读的解决方案是更改计数方法的签名:

public int count(GenericLinkList<E> list) {
    ...
}

答案 1 :(得分:0)

将您的点数方法更改为public int GenericLinkedList(Object o)

如果使用Object作为参数的类型,则只能调用Object上可用的方法。