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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29750009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:51:15  来源:igfitidea点击:

The method add(String) in the type ArrayList<String> is not applicable for the arguments (String[])

javaarraylist

提问by user3756933

I'm a beginner in JAVA and programming in general, so please be patient as I may not use the correct terms to correctly describe my doubts. I'll do my best, nevertheless. So, I have this ArrayList that I'm going to use regular expressions on, to split it on the commas. I really needed some help solving this problem, even if I have to change the way that I do the process. It's not important that it stays this way, it's the final result that matters the most to me. Thank you.

我是 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));
                }
            }
        }

回答by Joop Eggen

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

This uses the help class Collections to add a String[]by item.

这使用帮助类 Collections 添加一个String[]按项。

回答by Jean Logeart

temp.split(",")returns a String[].

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

List<String>.addtakes a Stringas a parameter, not an array of strings.

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

Yet you can use Collections.addAll, that takes an array as second argument:

但是您可以使用Collections.addAll, 将数组作为第二个参数:

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

You can alternatively use the addAll(Collection<String> c)method from the tempArrayList<String>but then you have to convert the array to a Collection:

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

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

回答by Bohemian

The code with the problem is essentially:

有问题的代码本质上是:

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

The problem is that split()returns a String[], but the list will only accept a String.

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

To fix, convert the array to a List and pass it to addAll():

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

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

or use the utility addAll()method:

或使用实用addAll()方法:

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

回答by silentprogrammer

Your Arraylist is of type Stringnot String[]and you are passing string array in add()because string.split()will give you array.Instead you can do

你的 Arraylist 的类型String不是String[],你正在传入字符串数组,add()因为string.split()会给你数组。相反,你可以做

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

回答by Jesper

The method add(String) in the type ArrayList is not applicable for the arguments (String[])

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

This means that you have an ArrayList<String>, and you are trying to call the addmethod with an arrayof strings (String[]) instead of with a single String. The method needs a single String, not an array.

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