简体   繁体   English

从字符串表达式(从ArrayList到Array)存储一格变量

[英]Storing First Case Variables from a String Expression (ArrayList to Array)

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. 您不能从ArrayList<Character>获取char[] ,只能获取Character[] ,并且装箱/拆箱不会使它们等效。

Have you considered using StringBuilder instead? 您是否考虑过使用StringBuilder 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[]. 由于ArrayList的类型为Character,因此可以将其转换为Character [],但是不能转换为char []。

So you can do something like: 因此,您可以执行以下操作:
Character[] x = new Character[letters.size()]; Character [] x =新的Character [letters.size()];
Character[] charArr = letters.toArray(x); Character [] charArr = letter.toArray(x);

OR 要么

Using stringbuilder would work: 使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM