1

我需要这个基本程序的帮助。我的朋友给了我大部分代码,但没有给我导入语句。在它说的最后list.add(finalCombined.get(counter3));,它给了我一个错误。我得到的错误是

Cannot find Symbol 
Symbol: variable list
location: class ArrayUtils

我对此很困惑。我添加了我认为需要的导入语句。谢谢

import java.util.ArrayList;
import java.util.Random;
import java.util.Collections;
import java.lang.String;

public class ArrayUtils {
    public void randomStrings(ArrayList<String> arrayList,int nbrOfStrings,int vowelCnt, int strSize){
        ArrayList <String> finalCombined = new ArrayList();
        ArrayList <String> finalCombined = new ArrayList();
        for (int cnt = 0; cnt < nbrOfStrings; cnt++)
        {
            int cnt2 = 0;
            int gn, size, vowelUsed;
            Random n1 = new Random();
            size = 122-96;
            //char cs[] = new char[strSize];
            //String cs;
            //cs = "";
            ArrayList <Character> cs = new ArrayList();
            int counter = 0;
            vowelUsed = 0;

            while (counter < 1)
            {
                vowelUsed = n1.nextInt(vowelCnt + 1);
                if (vowelUsed == 0)
                {
                }
                else
                {
                    counter = 2;
                }

            }

            while (cnt2 < (strSize - vowelUsed))
            {
                gn = n1.nextInt(size) + 97;

                if (gn == 97 | gn == 101 | gn == 105 | gn == 111 | gn == 117 | gn == 121)
                {
                }

                else
                {
                    cs.add((char)gn);
                    //cs += ((char)gn + "");
                    cnt2 ++;
                }
            }
            while (cnt2 < strSize)
            {
                gn = n1.nextInt(size) + 97;
                if (gn == 97 | gn == 101 | gn == 105 | gn == 111 | gn == 117 | gn == 121)
                {
                    cs.add((char)gn);
                    //cs += ((char)gn + "");
                    cnt2 ++;
                }
            }
            //int check;
            //check = list.add(cs[cnt]);
            Collections.shuffle(cs);

            String combined;
            combined = "";
            //System.out.println(cs);

            int counter2 = 0;
            while (counter2 < strSize)
            {
                combined += cs.get(counter2); 
                counter2 ++;
            }
            finalCombined.add(combined);
            counter2 = 0;
            combined = "";

        } // end # strings    

        for (int counter3 = 0; counter3 < nbrOfStrings; counter3++)
        {
            list.add(finalCombined.get(counter3));
        }

    } // end method
}
4

4 回答 4

2
list.add(finalCombined.get(counter3));

您尚未声明任何名为 的变量list,因此该行无法通过编译。

也许你的意思是

arrayList.add(finalCombined.get(counter3));

因为您的randomStrings方法有一个称为arrayList您根本没有使用的参数。

于 2015-04-15T12:05:34.280 回答
0

list我想你需要换成arrayListin line

list.add(finalCombined.get(counter3));

因为list没有在您的代码中的任何地方声明并且arrayList没有被使用。

于 2015-04-15T12:08:40.477 回答
0

您尚未定义名为 的变量listfinalCombined但是,您已经两次声明了具有相同名称的数组列表。一个变量名称应更改为list. 您还必须通过main以下方法调用方法。

new ArrayUtils().randomStrings(new ArrayList<String>(), 6, 3, 3);

我只是在徘徊arrayList方法中参数的目的是什么randomStrings。在方法中没有使用这个列表randomStrings

您的输出如下所示:

[aeo]
[aeo, yii]
[aeo, yii, isu]
[aeo, yii, isu, aea]
[aeo, yii, isu, aea, aaa]
[aeo, yii, isu, aea, aaa, iek]
于 2015-04-15T12:23:13.243 回答
0

始终正确且明智地声明变量,这有助于您的代码更具可读性和可理解性。这意味着您实际上是在减少编译器的压力;)

一个小建议:在编码时使用 IDE,它可以帮助您解决这样的编译时错误。此外,他们在建议解决方案和优化代码方面也非常友好..!

于 2016-11-16T16:14:48.053 回答