我有一个包含双值的字符串,例如:
String str = "0.1,0.4,0.9,0.1";
我想从这个字符串中得到一个双数组:
double[] arr = {0.1, 0.4, 0.9, 0.1};
对我来说最明显的方法是:
String[] tokens = str.split(",");
double[] arr = new double[tokens.length];
int i=0
for (String st : tokens) {
arr[i++] = Double.valueOf(st);
}
除了Java 8中提到的那个之外,还有更快/更好的方法吗?
答案 0 :(得分:7)
您可以使用double[] arr = Stream.of(str.split(","))
.mapToDouble (Double::parseDouble)
.toArray();
s:
byte[0] = 1d (in hexa) = 00011101 (in binary)
byte[1] = 32 = 00110010
byte[2] = d1 = 11010001
byte[3] =0x5 = 00000101