Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组是否为空?_Java_Arrays_Exception_Arraylist_Tostring - Fatal编程技术网

Java 数组是否为空?

Java 数组是否为空?,java,arrays,exception,arraylist,tostring,Java,Arrays,Exception,Arraylist,Tostring,所以我试图找出代码中ArrayOutOfBoundException错误的可能来源 代码的目的是将一堆卡片打印到记事本文件中,如下所示: public void printCardPiles (PrintWriter writer) { writer.println("Piles: "); for (int i = 0; i < piles.size(); i++) { writer.println(); MyArrayList

所以我试图找出代码中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());


    }

}
这是否意味着代码正在使用一个空数组(在这种情况下,空数组将是堆)。导致异常的原因是什么?还是别的什么

注意:卡的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;
}
我感谢大家的帮助,我知道这很模糊,但我可以根据需要提供更多细节。我主要是想知道从哪里开始寻找问题,或者是什么导致了问题

编辑:根据请求,我将添加整个Card类

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;
}
公共类卡实现可比性
{
//用于打印卡片级别
公共静态字符串[]列=
{
“A”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“J”、“Q”、“K”
};
//用于打印卡片套装
公共静态字符串[]=
{
“-C”、“-D”、“-H”、“-S”
};
//私有实例变量
私有整数秩;
私人诉讼;
//默认构造函数:初始化为俱乐部Ace
公共卡()
{
秩=1;
西服=1;
}
//双参数构造函数:初始化秩和suit
公共卡(int initRank,int initSuit)
{
秩=初始秩;
西服=西服;
}
//复制构造函数:从另一张卡复制一张卡
公共卡(其他卡)
{
otherCard.rank=等级;
otherCard.suit=套装;
}
//将此卡的相对位置返回给某张卡。
//这是对牌的比较,排名第一:A低,K高
//然后等级内的套装:梅花=1,钻石=2,心形=3,黑桃=4
公共整数比较(Card someCard)
{
如果(排名>someCard.rank)
{
如果(套装>某张卡片等级)
{
返回1;
}
否则如果(适合某张卡片等级)
{
返回1;
}
否则如果(适合某张卡片等级)
{
返回1;
}
否则如果(适合
}


请注意,我没有编写Card类的equals()或toString()。

我猜问题在于您使用的是复制构造函数。该问题使得实例将具有
等级
&
套装
为0。这会使
toString
无法正常工作

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;
}
public Card (Card otherCard)
{
    otherCard.rank = rank;
    otherCard.suit = suit;
}
这应该是

public Card (Card otherCard)
{
    this.rank = otherCard.rank;
    this.suit = otherCard.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我认为这条线导致了例外情况的发生

String printString = RANKS[rank - 1] + SUITS[suit - 1]; 
请确保
rank-1
suit-1
的值大于或等于0。
等级>=1和套装>=1

你能发布错误的完整堆栈跟踪吗如果
rank
(和/或
suit
)是
0
,那么你会得到该异常(它们有什么值?)发布你的详细信息
卡片
类请在主帖子中添加你的评论,希望这会有所帮助。让我们调试并检查
rank
suit
的值是多少。您可以发布错误的完整堆栈跟踪吗如果
rank
(和/或
suit
)是
0
,那么您将得到该异常(它们有哪些值?)发布你的详细信息
Card
class请在主帖子中添加你的评论,希望这会有所帮助。让我们调试并检查
rank
suit