java java中的字符串生成器到字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41327094/
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
String Builder to String Array in java
提问by Anonymous
I am trying to convert String builder to String array in java.I have used to Stringmethod to convert the Stringbuilder to String and then use splitmethod to convert it to String array.
我试图在java中将String builder转换为String数组。我已经习惯于String方法将Stringbuilder转换为String,然后使用split方法将其转换为String数组。
I am facing some issue in array size. The output of String builder is as follows: 0,,1,,,,,,,,,,2,,,
. Actually it contains 15 elements, but it's showing the size of stringbuilder as 18. And after converting to String array, it's showing size as 13.
我在数组大小方面遇到了一些问题。字符串生成器的输出如下:0,,1,,,,,,,,,,2,,,
。实际上它包含15个元素,但它显示stringbuilder的大小为18。转换为String数组后,它显示大小为13。
Am I doing wrong method? Is there is other method to convert String builder to String Array?
我做错了方法吗?是否有其他方法可以将 String builder 转换为 String Array?
StringBuilder output = new StringBuilder();
for (String str : listToSearch) {
int index = findIndex(headerArrayList, str);
if (index > -1) {
output.append(index);
} else {
output.append(str);
}
output.append(",");
}
String out = output.toString();
String[] destIndexArray = out.split(",");
The findIndex
method:
该findIndex
方法:
private static int findIndex(List<String> headerList, String element) {
for (int i = 0; i < headerList.size(); i++) {
if (headerList.get(i).equals(element)) {
return i;
}
}
return -1;
}
回答by Azodious
Am I doing wrong method?
我做错了方法吗?
There are many places your code need improvements. I'm suggesting some:
您的代码有很多地方需要改进。我推荐一些:
- Instead of re-inventing wheel, look for existing API: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object)
- 与其重新发明轮子,不如寻找现有的 API:https: //docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object)
- You can add only
String
s instead of index.
- 您只能添加
String
s 而不是索引。
for (String str :listToSearch) {
int index=headerArrayList.indexOf(str);
if (index < 0) {
output.append(str);
output.append(",");
}
// if(!headerArrayList.contains(str))
// output.append(str);
// output.append(",");
}
- Use
List<String>
instead ofStringBuilder
.
- 使用
List<String>
代替StringBuilder
。
Is there is other method to convert String builder to String Array?
是否有其他方法可以将 String builder 转换为 String Array?
Yes, but if you follow above points, you won't need it. but since you asked. See this: How can a StringBuilder best be converted to a String[]?
是的,但是如果您遵循以上几点,您就不需要它了。但既然你问了。请参阅:如何将 StringBuilder 最好地转换为 String[]?
回答by Bhagwati Malav
See using stringbuilder and again manipulating it and converting it into array is not a good idea. Instead insert everything into list of string, and to check if string is integer or not use this :
请参阅使用 stringbuilder 并再次操作它并将其转换为数组不是一个好主意。而是将所有内容插入字符串列表,并检查字符串是否为整数或不使用此:
str.matches("^-?\d+$");
And if you want to convert it into array you can do that
如果你想把它转换成数组,你可以这样做
String[] str = list.toArray(new String[list.size()])
回答by Tahmid Rahman
Have you simply tried in it with one line code
你有没有简单地用一行代码试过
stringBuilder.toString.split("delimiter")
?
stringBuilder.toString.split("delimiter")
?
Which is in your case, might look like
在你的情况下,可能看起来像
destIndexArray = output.toString.split(",")
If that doesnt work, then instead of converting into a StringBuilder, you can do this
如果这不起作用,那么您可以这样做而不是转换为 StringBuilder
List<String> output = new List<String>();
for (String str : listToSearch) {
int index = findIndex(headerArrayList, str);
if (index > -1) {
output.add(index);
} else {
output.add(str);
}
}
I think in this case you can do whatever with your list, no need of delimiter. Hope it helps!
我认为在这种情况下,您可以对列表执行任何操作,无需分隔符。希望能帮助到你!