java stackOverFlowError异常抛出1000次
背景:我的任务是编写一个程序,使用链表来跟踪纸牌游戏(战争)中的纸牌。所以我写了4个程序:card.java、deckofcards.java、hand.java、war.java(driver)。卡保存基本信息。当添加到卡片的链接列表时,我使用名为 setLast 的方法:
nextCard=null;
public card(String a, String b)
{
hand=a;
suit=b;
}
public void setLast(card c)
{
if(nextCard==null)
{
nextCard = c;
}
else
{
nextCard.setLast(c);
}
}
然后在 Deckofcards.java 中:
card deck, dealt;
public deckofcards()
{
rand = new Random();
dealt = new card("0","0"); //null card place holders
first = new card("0","0");
numcards = 52;
shuffle();
}
public card dealCard()
{
card c=new card("0","0");
if(first!= null)
{
c = first;
first = first.nextCard;
c.nextCard = null;
if(dealt.toString().compareTo("00")==0)
{
dealt = c;
}
else
{
dealt.setLast(c);
}
numcards--;
}
else
{
System.out.println("Deck: ran out of cards");
}
return c;
}
因此 Deckofcards 随机生成 52 张(无论如何,但这是一个不同的问题)有序卡片,并将它们处理给 hand.java 的实例在 war.java
hand.java:
card cards;
int numcards;
public void getCard(card c)
{
System.out.println("In hand.java,getCard");
if (numcards==0)
{
cards = c;
numcards++;
}
else
{
cards.setLast(c);
}
}
和 war.java:
players = new hand[numplayers];
for(int i=0;i<numplayers;i++)
{
players[i] = new hand();
}
deck = new deckofcards();
int i=0;
while(i<52)
{
int ii=0;
if((ii<numplayers)&&(i<52))
{
players[ii].getCard(deck.dealCard());
i++;
ii++;
}
else
{
ii=0;
}
}
中输出: hand.java中,getCard hand.java中,getCard 线程“main”中的异常 java.lang.StackOverflowError 在卡.setLast(卡.java:125) 在卡.setLast(卡.java:125) 在卡.setLast(卡.java:125) 在卡.setLast(卡.java:125) 在卡.setLast(卡.java:125) 在卡.setLast(卡.java:125) 在卡.setLast(卡.java:125) //重复1000次...
当然,我只提供了我认为导致问题的代码片段,我可以根据要求提供整个代码。
Background: my assignment was to write a program that uses linked list to keep track of cards in a card game (war). So I wrote 4 programs: card.java, deckofcards.java, hand.java, war.java(driver). card holds basic info. When adding to a linked list of the cards I use a method called setLast:
nextCard=null;
public card(String a, String b)
{
hand=a;
suit=b;
}
public void setLast(card c)
{
if(nextCard==null)
{
nextCard = c;
}
else
{
nextCard.setLast(c);
}
}
then in deckofcards.java:
card deck, dealt;
public deckofcards()
{
rand = new Random();
dealt = new card("0","0"); //null card place holders
first = new card("0","0");
numcards = 52;
shuffle();
}
public card dealCard()
{
card c=new card("0","0");
if(first!= null)
{
c = first;
first = first.nextCard;
c.nextCard = null;
if(dealt.toString().compareTo("00")==0)
{
dealt = c;
}
else
{
dealt.setLast(c);
}
numcards--;
}
else
{
System.out.println("Deck: ran out of cards");
}
return c;
}
so deckofcards generates 52 randomly(supposed to anyway, but thats a different question) ordered cards and hands deals them to instances of hand.java in war.java
hand.java:
card cards;
int numcards;
public void getCard(card c)
{
System.out.println("In hand.java,getCard");
if (numcards==0)
{
cards = c;
numcards++;
}
else
{
cards.setLast(c);
}
}
and war.java:
players = new hand[numplayers];
for(int i=0;i<numplayers;i++)
{
players[i] = new hand();
}
deck = new deckofcards();
int i=0;
while(i<52)
{
int ii=0;
if((ii<numplayers)&&(i<52))
{
players[ii].getCard(deck.dealCard());
i++;
ii++;
}
else
{
ii=0;
}
}
output:
In hand.java,getCard
In hand.java,getCard
Exception in thread "main" java.lang.StackOverflowError
at card.setLast(card.java:125)
at card.setLast(card.java:125)
at card.setLast(card.java:125)
at card.setLast(card.java:125)
at card.setLast(card.java:125)
at card.setLast(card.java:125)
at card.setLast(card.java:125)
//repeated 1000s of times...
Of course I only provided the snippet of code I believe is causing the problem I can provide the whole code upon request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的列表在某处有一个循环,即“A->B->C->D->B”。您使用“空卡占位符”而不是 null 值是否有原因?我注意到 setLast 正在检查空占位符的值“null”而不是“00”,但是如果没有看到所有代码,就很难判断列表在哪里出现循环。
Your list has a loop somewhere, i.e. "A->B->C->D->B". Is there a reason you are using "null card placeholders" instead of the value null? I notice setLast is checking for the value 'null' instead of '00' for your null placeholder, but without seeing all the code it's hard to tell where your list gets a loop at.