在 Java 中按制表符拆分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20105315/
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-08-12 23:11:12  来源:igfitidea点击:

Splitting by Tabs in Java

javastringsplit

提问by csharpener

I have a file scanned line by line into an ArrayList. I then create a new ArrayList in which I want to temporarily store that line into so that I may access certain values.

我有一个文件逐行扫描到一个 ArrayList 中。然后我创建了一个新的 ArrayList,我想在其中临时存储该行,以便我可以访问某些值。

Ex. IDname(tab)IDnumber(tab)vote(tab)date

前任。IDname(tab)IDnumber(tab)vote(tab)date

So, I create the temporary ArrayList named voteSubmission, and I go through every String in the fileRead array.

因此,我创建了名为 voteSubmission 的临时 ArrayList,并遍历了 fileRead 数组中的每个字符串。

Why is it that I get the error incompatible type for my split method?

为什么我的 split 方法出现错误类型不兼容?

ArrayList<String> voteSubmission = new ArrayList<String>(); 
for(String x : fileRead)
{
voteSubmission =  x.split("\t");
}

采纳答案by rgettman

The splitmethodreturns an array, not an ArrayList.

split方法返回一个数组,而不是一个ArrayList.

Either work with an array or convert it to an ArrayListmanually.

使用数组或ArrayList手动将其转换为数组。

回答by Kai Sternad

The error is: Type mismatch: cannot convert from String[] to ArrayList<String>

错误是: Type mismatch: cannot convert from String[] to ArrayList<String>

Which means that x.split("\t")provides a String[]array, but you assign it to an ArrayList.

这意味着x.split("\t")提供了一个String[]数组,但您将其分配给ArrayList.

If you'd like it to be an ArrayList, you'd have to convert it like this:

如果你希望它是一个 ArrayList,你必须像这样转换它:

new ArrayList<String>(Arrays.asList(x.split("\t")));

But since you are doing this in a loop it is likely that you would like to store all Arrays within the ArrayList. To do this, you have to create an ArrayList of type String[], like this:

但是由于您在循环中执行此操作,因此您可能希望将所有数组存储在 ArrayList 中。为此,您必须创建一个 String[] 类型的 ArrayList,如下所示:

ArrayList<String[]> voteSubmission = new ArrayList<String[]>(); 
for(String x : fileRead){
  voteSubmission.add((x.split("\t")));
}

回答by Rahul Tripathi

x.split("\t");this function will return an array not array list

x.split("\t");此函数将返回一个数组而不是数组列表

The splitfunction states that:

分割函数指出:

Splits this string around matches of the given regular expression.

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

Returns:

the array of strings computed by splitting this string around matches of the given regular expression

围绕给定正则表达式的匹配拆分此字符串。

此方法的工作方式就像通过使用给定表达式和零限制参数调用双参数 split 方法一样。因此,结果数组中不包含尾随空字符串。

返回:

通过围绕给定正则表达式的匹配拆分此字符串计算出的字符串数组

You may try to change your code like this:

您可以尝试像这样更改代码:

ArrayList<String> voteSubmission = new ArrayList<String>(); 
    for(String x : fileRead)
    {
        for(String value: x.split("\t"))
        {
            voteSubmission.add(value);
        }
    }

回答by Santhosh Gutta

The output of split() is of type string[] array and you are trying to assign to an ArrayList type, which is incompatible.

split() 的输出是 string[] 数组类型,您正尝试分配给不兼容的 ArrayList 类型。

Change your code to

将您的代码更改为

ArrayList<String> voteSubmission = new ArrayList<String>(); 
    for(String x : fileRead)
    {
        String arr[] = x.split("\t");
        if(arr != null && arr.length > 0)
        {
           for(String value: arr)
           {
              voteSubmission.add(value);
           }
        }
    }