java Java中将字符串转换为向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17829336/
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
Convert a String to a Vector in Java
提问by nami
I have a function that has an output as a 4 character String like "1000". I need to convert this into a vector of dimension (4,1) in order to be able to make computation with matrices. Any idea or help? Thank you very much in advance.
我有一个函数,它的输出为 4 个字符的字符串,如“1000”。我需要将其转换为维度 (4,1) 的向量,以便能够使用矩阵进行计算。任何想法或帮助?非常感谢您提前。
回答by xlecoustillier
Try:
尝试:
Vector<Character> v = new Vector<Character>(Arrays.asList(yourString.toCharArray()))
As stated by fge, a List would be at least as useful as a Vector:
正如 fge 所说,List 至少和 Vector 一样有用:
List<Character> l = Arrays.asList(yourString.toCharArray())
回答by Matthew Herbst
String s = "1000";
Vector myVec = new Vector();
//Convert the string to a char array and then just add each char to the vector
char[] sChars = s.toCharArray();
for(int i = 0; i < s.length(); ++i) {
myVec.add(sChars[i]);
}