I'm working on storing the first case of a variable into an array, since I do not know how many variables there will be I decided to use an array list.
I am getting the following error when compiling:
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
Here's my segment of code..
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;
}
You can't get a char[]
out of an ArrayList<Character>
, you can only get a Character[]
, and boxing/unboxing do not make those equivalent.
Have you considered using StringBuilder
instead? That's actually specifically designed for holding a mutable, unknown-length group of characters.
Since ArrayList is of type Character, you can convert it to Character[] but you cannot convert to char[].
Character[] x = new Character[letters.size()];
Character[] charArr = letters.toArray(x);
Using stringbuilder would work:
StringBuilder sb = new StringBuilder(letters.size());
for (Character c : letters)
sb.append(c);
String result = sb.toString();
return result.toCharArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.