我在使用Java创建链表时遇到了一些麻烦。我所遵循的所有指南都使用某种String类型的变量给出了示例,但是我必须创建的列表需要一个int类型。当我尝试调用position.link之类的东西时,使用int类型会产生错误消息,因为它说它无法将int转换为字符串。
为清楚起见,主程序应该让Scanner请求int,并使用该int创建一个创建每个节点的循环。我已经搞乱了迭代器和简单的单一链接列表,但我没有得到任何地方。
import java.util.NoSuchElementException;
public class SuitorLinkedList<Integer>
{
private class SuitorListNode
{
private int suitor;
private SuitorListNode link;
public SuitorListNode()
{
suitor = 0;
link = null;
}
public SuitorListNode(int newSuitor, SuitorListNode linkValue)
{
suitor = newSuitor;
link = linkValue;
}
} // End of SuitorListNode inner class
public class SuitorListIterator
{
public SuitorListNode position;
private SuitorListNode previous; // previous value of position
public SuitorListIterator()
{
position = head; // variable head of outer class
previous = null;
}
public void restart()
{
position = head;
previous = null;
}
public String next()
{
if(!hasNext())
throw new NoSuchElementException();
String toReturn = position.suitor;
previous = position;
position = position.link;
return toReturn;
}
public boolean hasNext()
{
return (position != null); // Throws IllegalStateExpression if false
} // Returns next value to be returned by next()
public String peak()
{
if(!hasNext())
throw new IllegalStateException();
return position.suitor;
}
public void addHere(int newData)
{
if(position == null && previous != null) // At end of list, add to end
previous.link = new SuitorListNode(newData, null);
else if(position == null || previous == null) // List empty or position is head node
head = new SuitorListNode(newData, head);
else // previous and position are consecutive nodes
{
SuitorListNode temp = new SuitorListNode(newData, position);
previous.link = temp;
previous = temp;
}
}
public void delete()
{
if(position == null)
throw new IllegalStateException();
else if (previous == null) // remove node at head
{
head = head.link;
position = head;
}
else // previous and position are consecutive nodes
{
previous.link = position.link;
position = position.link;
}
}
private SuitorListNode head;
}
public SuitorListIterator iterator()
{
return new SuitorListIterator();
}
}
每次尝试都会出现此错误,我已经尝试过搜索并使用toString()来帮助它,但它不会起作用:
SuitorLinkedList.java:60: error: incompatible types: int cannot be converted to String
return position.suitor;
^
我已经尝试创建常规链表并且做到了这一点:
public class SuitorList
{
public class SuitorNode
{
public int suitor;
public SuitorNode link;
public SuitorNode()
{
suitor = 0;
link = null;
} // Initialize veriables
public SuitorNode(int newSuitor, SuitorNode linkValue)
{
suitor = newSuitor;
link = linkValue;
} // Assigns values sent in from main
} // End inner class
private SuitorNode head; // Variable head of type SuitorNode (callback to Node program)
// Allows head to point to a node
public SuitorList()
{
head = null;
} // Initialize variables
// Memory space called head filled with null
public void addToStart(int suitorNum)
{
head = new SuitorNode(suitorNum, head);
}
// Creates node with head pointing to it at start of list
// head will have a definition as an object with a suitor and link = head
// If head = null, then link = null
// head is repositioned to point to node
public int size() // Reads size of list
{
int count = 0;
SuitorNode position = head; // Variable position of type SuitorNode will equal value at head; position points where head is pointing
while(position != null) // While list is not empty/ended
{
count++; // increase number of entries detected
position = position.link; // getLink will make position = link, leading to next entry in list
}
return count; // Display size.
}
public void outputList()
{
SuitorNode position = head; // Position points to same thing head points to
while(position != null) // While list is not empty/ended
{
System.out.println(position.suitor); // Print suitor
position = position.link; // Go to next entry
}
}
public void deleteNode(int count)
{
int moveCount = count - 1;
SuitorNode position = head;
while(head != link) // not winning
{
moveCount = count;
checkEnd(); // Checks for win before causing potential problem with 1 suitor left
checkTwoNumbersLeft(moveCount); // Takes care of movement when two nodes are left
checkEndNode(moveCount); // Checks when, for example, 2 nodes away
if(moveCount == count) // If checkEndNode and checkTwoNumbersLeft fail
{
position = position.link; // Move for first time
moveCount = moveCount - 1;
}
checkEnd();
checkEndNode2(moveCount); // When one movement is made already, deletes end node after
if(moveCount == moveCount - 1) // if checkEndNode2 fails
position = position.link.link; // 2nd deletion
count = moveCount;
}
isWinner();
} // End method deleteNode()
public void checkTwoNumbersLeft(int moveCount)
{
SuitorNode position;
if(position.link.link == null) // example: 1 5
{
createLoop();
position = position.link.link; // Deletes the 5
moveCount = moveCount - 2;
} // Used just in case only two numbers are present
} // End method checkTwoNumbersLeft()
public void checkEnd()
{
SuitorNode position;
if(position.link == null) // If at end of list
{
createLoop(); // creates a loop if the initial number has no next value
isWinner(); // If a 1 is used, the entire if statement will trigger
} // if true, head == link which will fall out of while in deleteNode()
} // End method checkEnd()
public void isWinner()
{
SuitorNode link;
SuitorNode position;
if(position == position.link)
{
head = link;
System.out.println("The winner is Suitor " + position + "!");
}
} // End method isWinner()
public void checkEndNode2(int moveCount)
{
SuitorNode position;
SuitorNode link;
if(position.link.link == null) // 1 movement
{
position.link = null;
createLoop();
isWinner();
moveCount = moveCount - 1;
}
} // End checkEndNode2()
public void checkEndNode(int moveCount)
{
SuitorNode position;
SuitorNode link;
if(position.link.link.link == null) // no movements
{
position = position.link;
position.link = null;
createLoop();
isWinner();
moveCount = moveCount - 2;
}
} // End checkEndNode()
public void createLoop()
{
SuitorNode position;
SuitorNode link;
if(link == null) // if at the end of the list
link = head; // Sets link to point to where head points, AKA beginning of list
} // End createLoop()
}
但是当我这样做时,变量 link , position 和 head 总是说它们没有被初始化,除非我把它们放在方法(如果我在列表中间调用方法,可能会弄乱我的代码。)
我的问题归结为1)我如何能够将int转换为字符串才能使用链表? 2)为什么程序中的变量 SuitorList 要求我在每个实例中重新初始化它们?我尝试将它们放在任何地方?
答案 0 :(得分:2)
问题是你的peek函数被定义为错误的类型
public String peak()
{
if(!hasNext())
throw new IllegalStateException();
return position.suitor;
}
它被定义为返回String
相反,它应定义为suitor
的类型,int
public int peak()
{
if(!hasNext())
throw new IllegalStateException();
return position.suitor;
}
与public String next()
相同的问题应该是public int next()
答案 1 :(得分:0)
变量 suitor 的变量类型为 int 。可以将转换为字符串。转换它;
public String peak() { if(!hasNext()) throw new IllegalStateException(); return Integer.toString(position.suitor); }
此方法应该摆脱该错误。或者使用String.valueOf(position.suitor);
。