[英]Storing First Case Variables from a String Expression (ArrayList to Array)
我正在将变量的第一种情况存储到数组中,因为我不知道会决定使用多少个数组列表。
编译时出现以下错误:
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;
}
您不能从ArrayList<Character>
获取char[]
,只能获取Character[]
,并且装箱/拆箱不会使它们等效。
您是否考虑过使用StringBuilder
? 实际上,这是专门为容纳可变的,长度未知的字符组而设计的。
由于ArrayList的类型为Character,因此可以将其转换为Character [],但是不能转换为char []。
因此,您可以执行以下操作:
Character [] x =新的Character [letters.size()];
Character [] charArr = letter.toArray(x);
要么
使用stringbuilder可以工作:
StringBuilder sb = new StringBuilder(letters.size());
for (Character c : letters)
sb.append(c);
String result = sb.toString();
return result.toCharArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.