Java - 更改原始类型变量的值.可能?

Kar*_*rim 3 java types list nodes

我首先要说的是我是一个Java(/编程)新手,这是我在网站上的第一个问题.

刚学会了如何使用递归节点在Java中创建有序列表.一切都很简单,直到我遇到这个练习,要求我编写一个方法,将每个节点中包含的任何值加倍.这是我试写的代码:

public class ListaInteri<E extends Integer>
{
    private NodoLista inizio;

    // Private inner class each instance of these has a raw type variable and a
    // ref
    // to next node in the list
    private class NodoLista
    {
        E dato;
        NodoLista pros;
    }

    // method that adds whatever is meant by x to the begging of the list

    public void aggiungi(E x)
    {
        NodoLista nodo = new NodoLista();
        nodo.dato = x;
        if (inizio != null)
            nodo.pros = inizio;
        inizio = nodo;
    }

    // a method that switches last and first elements in the list
    public void scambia()
    {
        E datoFine;

        if (inizio != null && inizio.pros != null) {
            E datoInizio = inizio.dato;
            NodoLista nl = inizio;

            while (nl.pros != null)
                nl = nl.pros;

            datoFine = nl.dato;
            inizio.dato = datoFine;
            nl.dato = datoInizio;
        }
    }

    // and here is the problem
    // this method is supposed to double the value of the raw type variable dato
    // of each node
    public void raddoppia()
    {

        if (inizio != null) {
            NodoLista temp = inizio;
            while (temp != null) {
                temp.dato *= 2;
            }
        }
    }

    // Overriding toString from object (ignore this)
    public String toString(String separatore)
    {
        String stringa = "";
        if (inizio != null) {
            stringa += inizio.dato.toString();
            for (NodoLista nl = inizio.pros; nl != null; nl = nl.pros) {
                stringa += separatore + nl.dato.toString();
            }
        }
        return stringa;
    }

    public String toString()
    {
        return this.toString(" ");
    }

}
Run Code Online (Sandbox Code Playgroud)

这是编译器给我的错误.

    ListaInteri.java:39: inconvertible types
found   : int
required: E
  temp.dato*=2;         
             ^
1 error
Run Code Online (Sandbox Code Playgroud)

现在请记住,任何形式的帮助都会受到高度赞赏,无论如何这里是我想要回答的问题.

  1. 为什么会这样?在编译期间是否存在类型擦除原始类型的事情,其中​​忽略了与参数或参数类型有关的所有信息?
  2. 我该如何解决?第二种方法(首先和最后一种方法)显示,只要它被另一个原始类型传递,编译器就可以更改节点中的字段,而如果我们尝试将它乘以2,例如它就不再可以了因为编译器现在知道我们正在讨论一个int/Integer,所以给出了这个错误.提前感谢您的任何答案.

编辑; 抱歉必须让它可读现在应该没问题.EDIT2:几乎可读的唉!

Mic*_*rdt 6

Integer是最后的,它不能延长.因此,您对泛型的使用毫无意义(虽然语法上有效),您也可以使用int- 然后问题就会消失.