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)
现在请记住,任何形式的帮助都会受到高度赞赏,无论如何这里是我想要回答的问题.
编辑; 抱歉必须让它可读现在应该没问题.EDIT2:几乎可读的唉!
| 归档时间: |
|
| 查看次数: |
1179 次 |
| 最近记录: |