Java - changing the value of a variable of type raw. Possible?

To begin with, I'm new to Java (/ programming), and this is my first question on a website.

I just learned how to create an ordered list in Java using recursive nodes. Everything was easy until I came across this exercise and asked to write a method that would double any value contained in each node. Here is the code I tried to write:

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(" ");
    }

}

and here is the error that the compiler gives.

    ListaInteri.java:39: inconvertible types
found   : int
required: E
  temp.dato*=2;         
             ^
1 error

Now keep in mind that any help would be greatly appreciated in any case, these are questions that I would like to answer.

  • Why is this happening? Isn’t there such a thing as erasing the types of raw types at compile time, when all information related to parameters or types of arguments is ignored?
  • ? (, ) , , , , 2, , , int/Integer, . .

; , , . EDIT2: argh!

+3
5

Integer , . ( ), int - .

+6

( , , ).

, , , . Integer - , , E extends Integer.

+3

, , - : :

, , , ?

Java, , Java ++ , (Java 5 ) Java.

, " , , " - , . , , " ", , , , .

: ? - , . Integer , ArrayList<T> , ArrayList<Integer> ArrayList<String>. , , , , ArrayList<Integer> ArrayList<String> ArrayList. if (foo instanceof String) Java, if (foo instanceof ArrayList<String>): if , ArrayList<String>, ArrayList<Integer> ArrayList.

: ? , ( "-" ) . , Java, Java :

ArrayList<String> foo = new ArrayList<String>();
foo.add(new Integer(3)); // Error - Compiler knows that String is expected

, , , .: -)

: <E extends Integer>? ? Eclipse, <E extends Integer> E Integer, .

(, raddoppia - , , <E extends Integer> - ? raddoppia , , ...)

+1

:

  temp.dato= new Integer(((Integer)temp.dato).intValue*2);
0

,

<E extends Number>

tmp.dato = tmp.dato.intValue() * 2;

, , - , .

0

Source: https://habr.com/ru/post/1733620/


All Articles