数组是空的吗?
Is the array empty?
所以我试图找出代码中 ArrayOutOfBoundException 错误的可能来源。
代码的目的是将一堆卡片打印到记事本文件中,如下所示:
public void printCardPiles (PrintWriter writer)
{
writer.println("Piles: ");
for (int i = 0; i < piles.size(); i++)
{
writer.println();
MyArrayList<Card> tester = piles.get(i).getList();
writer.print(tester.toString());
}
}
问题是我不断收到 ArrayIndexOutOfBoundsException,但我不太清楚源是从哪里来的。
我注意到即使我将代码更改为:
public void printCardPiles (PrintWriter writer)
{
writer.println("Piles: ");
for (int i = 0; i < 1; i++) // CHANGE: i < piles.size() to i < 1
{
writer.println();
MyArrayList<Card> tester = piles.get(i).getList();
writer.print(tester.toString());
}
}
无论如何,我仍然遇到越界异常。
异常指向行 writer.print(tester.toString());
那么这可能是来源吗?确切的例外情况如下(为上下文添加了注释):
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Card.toString(Card.java:134) // Sets up a card object
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at MyArrayList.toString(MyArrayList.java:168)
at Table.printCardPiles(Table.java:199) // The exception location
at SortCardGame.playGame(SortCardGame.java:113) // Where the exception's method is being called from
at SortCardGame.main(SortCardGame.java:52)
这是否意味着代码正在使用一个空数组(在这种情况下,空数组将是一堆)。是什么导致了异常?或者是别的什么?
注意:卡片的 toString() 也如下所示:
public String toString()
{
String printString = RANKS[rank - 1] + SUITS[suit - 1]; // RANKS is the the array holding all the possible Ranks
// rank is what rank the current card is
// Same as the above
return printString;
}
感谢大家的帮助,我知道这很模糊,但我可以根据需要提供更多详细信息。我主要是在寻找一些关于从哪里开始寻找问题的想法,或者可能导致问题的原因。
编辑: 根据要求,我添加了整个卡片 Class。
public class Card implements Comparable<Card>
{
// For printing the card rank
public static String[] RANKS =
{
" A", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", " J", " Q", " K"
};
// For printing the card suit
public static String[] SUITS =
{
"-C", "-D", "-H", "-S"
};
// private instance variables
private int rank;
private int suit;
// Default Constructor: initialize to Ace of Clubs
public Card()
{
rank = 1;
suit = 1;
}
// Two-Param Constructor : initialize rank and suit
public Card (int initRank, int initSuit)
{
rank = initRank;
suit = initSuit;
}
// Copy constructor: Copies a Card from another Card
public Card (Card otherCard)
{
otherCard.rank = rank;
otherCard.suit = suit;
}
// Returns relative position of this Card to someCard.
// This compares the Cards, first by rank: Aces low, King High
// then Suit within rank: CLUB=1, DIAMOND=2, HEART=3, SPADE=4
public int compareTo (Card someCard)
{
if (rank > someCard.rank)
{
if (suit > someCard.rank)
{
return 1;
}
else if (suit < someCard.rank)
{
return -1;
}
else
{
return 0;
}
}
else if (rank < someCard.rank)
{
if (suit > someCard.rank)
{
return 1;
}
else if (suit < someCard.rank)
{
return -1;
}
else
{
return 0;
}
}
else
{
if (suit > someCard.rank)
{
return 1;
}
else if (suit < someCard.rank)
{
return -1;
}
else
{
return 0;
}
}
}
// Determines whether this Card has the same rank and suit
// as another Card passed in - Note cast of obj to Card
@Override
public boolean equals (Object obj)
{
Card someCard = (Card) obj;
return (someCard.compareTo (this) == 0);
}
// Print the card's rank and suit using the static
// String arrays defined about
public String toString()
{
String printString = RANKS[rank - 1] + SUITS[suit - 1];
return printString;
}
}
快速说明,我没有写卡片的 equals() 或 toString() Class。
我猜你是使用拷贝构造函数的问题。该问题使实例的 rank
和 suit
为 0。这使得 toString
无法正常工作
public Card (Card otherCard)
{
otherCard.rank = rank;
otherCard.suit = suit;
}
这应该是
public Card (Card otherCard)
{
this.rank = otherCard.rank;
this.suit = otherCard.suit;
}
我认为异常是因为这条线
String printString = RANKS[rank - 1] + SUITS[suit - 1];
请确保rank-1
和suit-1
的值大于等于0。
等级>=1 且花色>=1
所以我试图找出代码中 ArrayOutOfBoundException 错误的可能来源。
代码的目的是将一堆卡片打印到记事本文件中,如下所示:
public void printCardPiles (PrintWriter writer)
{
writer.println("Piles: ");
for (int i = 0; i < piles.size(); i++)
{
writer.println();
MyArrayList<Card> tester = piles.get(i).getList();
writer.print(tester.toString());
}
}
问题是我不断收到 ArrayIndexOutOfBoundsException,但我不太清楚源是从哪里来的。
我注意到即使我将代码更改为:
public void printCardPiles (PrintWriter writer)
{
writer.println("Piles: ");
for (int i = 0; i < 1; i++) // CHANGE: i < piles.size() to i < 1
{
writer.println();
MyArrayList<Card> tester = piles.get(i).getList();
writer.print(tester.toString());
}
}
无论如何,我仍然遇到越界异常。
异常指向行 writer.print(tester.toString());
那么这可能是来源吗?确切的例外情况如下(为上下文添加了注释):
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Card.toString(Card.java:134) // Sets up a card object
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at MyArrayList.toString(MyArrayList.java:168)
at Table.printCardPiles(Table.java:199) // The exception location
at SortCardGame.playGame(SortCardGame.java:113) // Where the exception's method is being called from
at SortCardGame.main(SortCardGame.java:52)
这是否意味着代码正在使用一个空数组(在这种情况下,空数组将是一堆)。是什么导致了异常?或者是别的什么?
注意:卡片的 toString() 也如下所示:
public String toString()
{
String printString = RANKS[rank - 1] + SUITS[suit - 1]; // RANKS is the the array holding all the possible Ranks
// rank is what rank the current card is
// Same as the above
return printString;
}
感谢大家的帮助,我知道这很模糊,但我可以根据需要提供更多详细信息。我主要是在寻找一些关于从哪里开始寻找问题的想法,或者可能导致问题的原因。
编辑: 根据要求,我添加了整个卡片 Class。
public class Card implements Comparable<Card>
{
// For printing the card rank
public static String[] RANKS =
{
" A", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", " J", " Q", " K"
};
// For printing the card suit
public static String[] SUITS =
{
"-C", "-D", "-H", "-S"
};
// private instance variables
private int rank;
private int suit;
// Default Constructor: initialize to Ace of Clubs
public Card()
{
rank = 1;
suit = 1;
}
// Two-Param Constructor : initialize rank and suit
public Card (int initRank, int initSuit)
{
rank = initRank;
suit = initSuit;
}
// Copy constructor: Copies a Card from another Card
public Card (Card otherCard)
{
otherCard.rank = rank;
otherCard.suit = suit;
}
// Returns relative position of this Card to someCard.
// This compares the Cards, first by rank: Aces low, King High
// then Suit within rank: CLUB=1, DIAMOND=2, HEART=3, SPADE=4
public int compareTo (Card someCard)
{
if (rank > someCard.rank)
{
if (suit > someCard.rank)
{
return 1;
}
else if (suit < someCard.rank)
{
return -1;
}
else
{
return 0;
}
}
else if (rank < someCard.rank)
{
if (suit > someCard.rank)
{
return 1;
}
else if (suit < someCard.rank)
{
return -1;
}
else
{
return 0;
}
}
else
{
if (suit > someCard.rank)
{
return 1;
}
else if (suit < someCard.rank)
{
return -1;
}
else
{
return 0;
}
}
}
// Determines whether this Card has the same rank and suit
// as another Card passed in - Note cast of obj to Card
@Override
public boolean equals (Object obj)
{
Card someCard = (Card) obj;
return (someCard.compareTo (this) == 0);
}
// Print the card's rank and suit using the static
// String arrays defined about
public String toString()
{
String printString = RANKS[rank - 1] + SUITS[suit - 1];
return printString;
}
}
快速说明,我没有写卡片的 equals() 或 toString() Class。
我猜你是使用拷贝构造函数的问题。该问题使实例的 rank
和 suit
为 0。这使得 toString
无法正常工作
public Card (Card otherCard)
{
otherCard.rank = rank;
otherCard.suit = suit;
}
这应该是
public Card (Card otherCard)
{
this.rank = otherCard.rank;
this.suit = otherCard.suit;
}
我认为异常是因为这条线
String printString = RANKS[rank - 1] + SUITS[suit - 1];
请确保rank-1
和suit-1
的值大于等于0。
等级>=1 且花色>=1