类型为ArrayList <string>的方法add(String)不适用于参数(String [])</string>

时间:2015-04-20 13:59:13

标签: java arraylist

我是JAVA的初学者和一般的编程,所以请耐心等待,因为我可能没有使用正确的术语来正确描述我的疑虑。不过,我会尽我所能。 所以,我有这个ArrayList,我将使用正则表达式,将它拆分为逗号。 我真的需要一些帮助来解决这个问题,即使我必须改变我做这个过程的方式。保持这种方式并不重要,这对我来说是最重要的结果。 谢谢。

  String temp; 
    String temp2;
    ArrayList <String> tempsplit = new ArrayList<String> (); 
    ArrayList <String> dominios = new ArrayList<String> (); {

    for (int h = 0; h < 191; h++){
        temp = features.get(h);
        **temp2.add(temp.split(","));
        tempsplit.add(temp.split(","));** 
        //in these last couple lines I get the error "The method add(String) in the type ArrayList<String> is not applicable for the arguments (String[])" 
        for(int oi = 0; oi < tempsplit.size(); oi++){
            for (int t = 0; t < dominios.size() ; t++){
                int conf = 0;
                if (tempsplit.get(oi) == dominios.get(t)){
                    conf = 0;           
                    }
                else{ conf = 1;
        }
                if (conf == 1){
                    dominios.add (tempsplit.get(oi));
                }
            }
        }

5 个答案:

答案 0 :(得分:3)

Collections.addAll(temp2, temp.split(","));

这使用帮助类集合按项添加String[]

答案 1 :(得分:2)

temp.split(",")会返回String[]

List<String>.addString作为参数,而不是字符串数组。

然而,您可以使用Collections.addAll,它将数组作为第二个参数:

Collections.addAll(temp2, temp.split(","));

您也可以使用addAll(Collection<String> c) temp中的ArrayList<String>方法,但必须将数组转换为Collection

temp2.addAll(Arrays.asList(temp.split(",")));

答案 2 :(得分:1)

有问题的代码基本上是:

ArrayList <String> tempsplit = new ArrayList<String>();   
tempsplit.add(temp.split(",")); // compile error

问题是split()会返回String[],但该列表只接受String

要修复,请将数组转换为List并将其传递给addAll()

tempsplit.addAll(Arrays.asList(temp.split(",")));

或使用实用程序addAll()方法:

Collections.addAll(tempsplit, temp.split(","));

答案 3 :(得分:0)

您的Arraylist类型为String而不是String[],您在add()传递字符串数组,因为string.split()会为您提供数组。相反,您可以

for(String a:temp.split(",")){
 tempsplit.add(a);
}

答案 4 :(得分:0)

  

ArrayList类型中的方法add(String)不适用于参数(String [])

这意味着您有一个ArrayList<String>,并且您尝试使用数组字符串(add)调用String[]方法,而不是一个String。该方法需要一个String,而不是一个数组。

相关问题