gpt4 book ai didi

java - 从字符串表达式(ArrayList 到 Array)存储 First Case 变量

转载 作者:行者123 更新时间:2023-11-30 07:19:25 26 4
gpt4 key购买 nike

我正在努力将变量的第一个案例存储到数组中,因为我不知道会有多少个变量,所以我决定使用数组列表。

编译时出现以下错误:

javac TruthTester.java
TruthTester.java:102: error: no suitable method found for toArray(char[])
char[] charArr = letters.toArray(x);
^
method ArrayList.<T>toArray(T[]) is not applicable
(inferred type does not conform to declared bound(s)
inferred: char bound(s): Object)
method ArrayList.toArray() is not applicable
(actual and formal argument lists differ in length)
where T is a type-variable:
T extends Object declared in method <T>toArray(T[])
1 error

这是我的代码段..

private static char[] getVariables(String[] lines)
{
ArrayList<Character> letters = new ArrayList<Character>();

int count = 0;
for(int x = 0; x < lines.length; x++)
{
for(int i = 0; i < lines[x].length(); i++)
{
char tempStore = 'a';
boolean isCopy = false;
int counter = 0;
for(char letter : letters)
{
if(isAlphabeticToken(letter)){
if(lines[x].charAt(counter) == letter)
{
isCopy = true;
}
else
{
isCopy = false;
tempStore = letter;
}
}
else
{
isCopy = true;
}
counter++;
}
if(!isCopy)
{
letters.add(tempStore);
count++;
}
}
}
/*
* ==========
* ERROR HERE
* ==========
*/
char[] x = new char[letters.size()];
char[] charArr = letters.toArray(x);

return charArr;
}

最佳答案

你无法获得 char[]从一个ArrayList<Character> , 你只能得到一个 Character[] ,装箱/拆箱不会使它们等同。

您是否考虑过使用 StringBuilder反而?这实际上是专门为保存可变的、未知长度的字符组而设计的。

关于java - 从字符串表达式(ArrayList 到 Array)存储 First Case 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14696933/

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