gpt4 book ai didi

java - 将逗号分隔的字符串转换为尾随逗号分隔的数组列表

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:05:18 29 4
gpt4 key购买 nike

如何将下面的字符串转成java ArrayList?

我试过一行:

"GOC,,404719160795907,911367109182460,218.248.72.40,62944,31,3331,31425,3544151354,117.200.252.120,bsnlnet,2,100.115.103.86,0,0,0,20190225123602,20190225125558,1196,,0,,,,,5428665,0,mnc071.mcc404.gprs,9448861612,Prepaid,1,,,2,255,,,,"

List<String> data = new ArrayList<>(Arrays.asList(line.split(",")) )

但问题是它正在转换到 255(列)并且数组的大小已经达到 36,但它应该是 40

最佳答案

来自 String.split(String) 的文档:

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.

您会看到空字符串未包含在您的数组中,并且因为您的 line,,,, 结尾,您将丢失这 4 个元素,剩下大小为 36 的 List

要包含最后一个元素,您需要调用重载 String.split(String, int),如下所示:

List<String> data = new ArrayList<>(Arrays.asList(line.split(",", -1)));

来自文档:

[...] If n (the second parameter to split()) is non-positive then the pattern will be applied as many times as possible and the array can have any length. [...]

关于java - 将逗号分隔的字符串转换为尾随逗号分隔的数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54882669/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com