gpt4 book ai didi

java - Java 中的类型转换

转载 作者:行者123 更新时间:2023-12-01 19:18:32 24 4
gpt4 key购买 nike

我想知道是否有一种方法可以在java中将整数数组转换为字符数组。我不断收到 nullPointer Exception 错误。

这就是我所拥有的......

    private char [] clubsArray; // Will hold the clubsString String as an array 
private char [] heartsArray;// Will hold the heartsString String as an array
private char [] spadesArray;// Will hold the spadesString String as an array
private char [] diamondsArray;// Will hold the diamondsString String as an array
private int [] intClubsArray; // Holds the character array as number values so I can sort it properly
private int [] intHeartsArray; // Holds the character array as number values so I can sort it properly
private int [] intSpadesArray; // Holds the character array as number values so I can sort it properly
private int [] intDiamondsArray; // Holds the character array as number values so I can sort it properly


public void cast()
{
for (int i = 0; i < clubsArray.length; i++)// A loop that will cast all the clubs into ints
{
intClubsArray[i] = (int) clubsArray[i];
}
for (int i = 0; i < heartsArray.length; i++)// A loop that will cast all the hearts into ints
{
intHeartsArray[i] = (int) heartsArray[i];
}
for (int i = 0; i < spadesArray.length; i++)// A loop that will cast all the spades into ints
{
intSpadesArray[i] = (int) spadesArray[i];
}
for (int i = 0; i < diamondsArray.length; i++)// A loop that will cast all the diamonds into ints
{
intDiamondsArray[i] = (int) diamondsArray[i];
}
}

为什么我会收到此错误?可以修好吗??我可以转换整个数组吗?此外,我不想为变量设置特定的大小,因为当它变成 int [] 时,我会在空白处得到不必要的 0 。

最佳答案

错误是在 clubsArray.length 处吗?

如果是这样,那是因为您还没有执行以下操作:

clubsArray = new char[12];

您也可能无法初始化 intClubsArray 或任何其他数组)

关于java - Java 中的类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5533448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com