Java 单链表中的泛型返回类型

Java 单链表中的泛型返回类型,java,generics,linked-list,Java,Generics,Linked List,我正在开发一个程序,该程序使用通用对象的链表,在定义单个列表的类中,有一个方法返回该链接的值: public class Link<E> { private E _value; public E getValue() { return _value; } 公共类链接{ 私人E_值; 公共E getValue(){ 返回_值; } 然后我想在linked link类中使用类型的返回值来返回列表中某个位置的链接值 publ

我正在开发一个程序,该程序使用通用对象的链表,在定义单个列表的类中,有一个方法返回该链接的值:

 public class Link<E> {
      private E _value;

      public E getValue() {
           return _value;
      }
公共类链接{
私人E_值;
公共E getValue(){
返回_值;
}
然后我想在linked link类中使用类型的返回值来返回列表中某个位置的链接值

public class LinkedList <E> implements List <E> {
    public E get(int index){
        Link current = goTo(index); //go to is a helper method that returns a pointer to a Link at position index
        E value = current.getValue(); //this is the source of the problem
        return value;

    }

}
公共类LinkedList实现列表{
公共E-get(int索引){
Link current=goTo(index);//go to是一个助手方法,返回指向位置索引处链接的指针
E value=current.getValue();//这就是问题的根源
返回值;
}
}
这个问题与泛型类型有关,我不完全理解它是如何工作的

更新


我收到消息:
类型不匹配:无法在源代码中指示的行上从Object转换为E

在实现类型参数时,必须为其提供实际类型。下面给出了一个示例

package com.example.transformer;

/**
 * This represents the transformer contract to transform domain objects into
 * data transfer objects.
 *
 * @param <S>
 *            source to be transformed.
 * @param <T>
 *            result after the transformation.
 */
public interface Transformer<S, T> {
    T transform(S source);

}

希望这有帮助。祝您编码愉快。

我认为您的问题在于这一行:Link current=goTo(index);您必须更正它以指定要使用的对象类型:
Link current=goTo(index);
replace“使用goTo方法的返回类型值的TypeOfObject。我假设它是一个整数,因此如果是这种情况,只需将
链接电流=goTo(索引);
替换为
链接电流=goTo(索引);

看起来您应该这样做:

Link<E> current = goTo(index);
链路电流=转到(索引);

正如
Link current
(不带
)指向对象类的列表一样。

问题出在哪里?您忘记告诉我们了。您正在尝试实现列表,但列表不是一个接口?您是否应该改为使用扩展列表?列表是一个接口。问题出在E value=current.getValue();我得到以下消息:类型不匹配:无法从对象转换为EIt不必为空,如果您将其保留为空,它将成为对象类。但仍然不能传递E左右。goTo(索引)返回什么类型的值?字符串?整数?替换为goTo方法的返回类型。
public class LinkedList <YourType> implements List <E> {
   // your code goes here ...
}
Link<E> current = goTo(index);