Currently I have this:
public class sequence
{
private double[] sequence;
// set up sequence by parsing s
//the numbers in s will be seperated by commas
public Sequence(String s)
{
String [] terms = s.split (",");
sequence = Double.parseDouble(terms);
}
}
What I have does not work. But basically what i am trying to achieve is to move the numerical terms in String s (such as 1,2,3,4,5,6) in an array of double called sequence.
You need to iterate over the terms.
String [] terms = s.split (",");
sequence = new double[terms.length];
for (int i = 0; i < terms.length; i++) {
sequence[i] = Double.parseDouble(terms[i]);
}
Double.parseDouble
take one String
and returns one Double
. You are passing an array of Strings. Change you code to pass just one String.
public Sequence(String s) {
String [] terms = s.split (",");
sequence = new Double[terms.length];
for (int i = 0; i < terms.length; i++) {
sequence[i] = Double.parseDouble(terms[i]);
}
}
使用Java 8,您可以执行以下操作
double[] sequence = Stream.of(s.split(",")).mapToDouble(Double::parseDouble).toArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.