How to convert a String without separator to an ArrayList<Character>
.
如何将一个没有分隔符的字符串转换为ArrayList <字符> 。
My String is like this:
我的弦是这样的:
String str = "abcd..."
I know one way of doing this is converting the String to char[]
first, and then convert the char []
to ArrayList <Character>
.
我知道一种方法是将字符串转换为char[],然后将char[]转换为ArrayList <字符> 。
Is there any better way to do this? like converting directly? Considering time and performance, because I am coding with a big database.
还有什么更好的办法吗?喜欢直接转换吗?考虑到时间和性能,因为我在用一个大数据库编码。
7 个解决方案
#1
10
You need to add it like this.
你需要像这样添加它。
String str = "abcd...";
ArrayList<Character> chars = new ArrayList<Character>();
for (char c : str.toCharArray()) {
chars.add(c);
}
#2
4
use lambda expression to do this.
用lambda表达式来做这个。
String big_data = "big-data";
ArrayList<Character> chars
= new ArrayList<>(
big_data.chars()
.mapToObj(e -> (char) e)
.collect(
Collectors.toList()
)
);
#3
2
If you dn not need to modify list after it created, probably the better way would be to wrap string into class implementing List<Character>
interface like this:
如果您不需要在创建列表后修改列表,那么可能更好的方法是将字符串打包到类实现列表 <字符> 接口如下:
import java.util.AbstractList;
import java.util.List;
public class StringCharacterList extends AbstractList <Character>
{
private final String string;
public StringCharacterList (String string)
{
this.string = string;
}
@Override
public Character get (int index)
{
return Character.valueOf (string.charAt (index));
}
@Override
public int size ()
{
return string.length ();
}
}
And then use this class like this:
然后使用这个类:
List <Character> l = new StringCharacterList ("Hello, World!");
System.out.println (l);
#4
1
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abcd...";
ArrayList<Character> a=new ArrayList<Character>();
for(int i=0;i<str.length();i++)
{
a.add(str.charAt(i));
}
System.out.println(a);
}
#5
1
Sorry for the Retrobump, but this is now really easy!
抱歉,这是逆行,但现在真的很简单!
You can do this easily in Java 8 using Streams! Try this:
您可以在Java 8中使用流轻松地完成这项工作!试试这个:
String string = "testingString";
List<Character> list = string.chars().mapToObj((i) -> Character.valueOf((char)i)).collect(Collectors.toList());
System.out.println(list);
You need to use mapToObj
because chars()
returns an IntStream
.
您需要使用mapToObj,因为chars()返回一个IntStream。
#6
0
you can do it like this:
你可以这样做:
import java.util.ArrayList;
public class YourClass{
public static void main(String [] args){
ArrayList<Character> char = new ArrayList<Character>();
String str = "abcd...";
for (int x = 0; x < str.length(); x ++){
char.add(str.charAt(x));
}
}
}
#7
0
There is one-line guava solution:
有一行的guava解决方案:
Lists.charactersOf("hello");
You can pass resulting ImmutableList
to constructor of ArrayList
but may I ask why do you need exactly ArrayList?
您可以将生成的ImmutableList传递给ArrayList的构造函数,但是我可以问您为什么需要精确的ArrayList?
#1
10
You need to add it like this.
你需要像这样添加它。
String str = "abcd...";
ArrayList<Character> chars = new ArrayList<Character>();
for (char c : str.toCharArray()) {
chars.add(c);
}
#2
4
use lambda expression to do this.
用lambda表达式来做这个。
String big_data = "big-data";
ArrayList<Character> chars
= new ArrayList<>(
big_data.chars()
.mapToObj(e -> (char) e)
.collect(
Collectors.toList()
)
);
#3
2
If you dn not need to modify list after it created, probably the better way would be to wrap string into class implementing List<Character>
interface like this:
如果您不需要在创建列表后修改列表,那么可能更好的方法是将字符串打包到类实现列表 <字符> 接口如下:
import java.util.AbstractList;
import java.util.List;
public class StringCharacterList extends AbstractList <Character>
{
private final String string;
public StringCharacterList (String string)
{
this.string = string;
}
@Override
public Character get (int index)
{
return Character.valueOf (string.charAt (index));
}
@Override
public int size ()
{
return string.length ();
}
}
And then use this class like this:
然后使用这个类:
List <Character> l = new StringCharacterList ("Hello, World!");
System.out.println (l);
#4
1
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abcd...";
ArrayList<Character> a=new ArrayList<Character>();
for(int i=0;i<str.length();i++)
{
a.add(str.charAt(i));
}
System.out.println(a);
}
#5
1
Sorry for the Retrobump, but this is now really easy!
抱歉,这是逆行,但现在真的很简单!
You can do this easily in Java 8 using Streams! Try this:
您可以在Java 8中使用流轻松地完成这项工作!试试这个:
String string = "testingString";
List<Character> list = string.chars().mapToObj((i) -> Character.valueOf((char)i)).collect(Collectors.toList());
System.out.println(list);
You need to use mapToObj
because chars()
returns an IntStream
.
您需要使用mapToObj,因为chars()返回一个IntStream。
#6
0
you can do it like this:
你可以这样做:
import java.util.ArrayList;
public class YourClass{
public static void main(String [] args){
ArrayList<Character> char = new ArrayList<Character>();
String str = "abcd...";
for (int x = 0; x < str.length(); x ++){
char.add(str.charAt(x));
}
}
}
#7
0
There is one-line guava solution:
有一行的guava解决方案:
Lists.charactersOf("hello");
You can pass resulting ImmutableList
to constructor of ArrayList
but may I ask why do you need exactly ArrayList?
您可以将生成的ImmutableList传递给ArrayList的构造函数,但是我可以问您为什么需要精确的ArrayList?