假设我有以下一行:
ball_velocities:45,500 46,500 47,500
我想:
String[] numbers = data.split("\\\\s+");
if (numbers.length > 0) {
List<Velocity> velocities = new ArrayList<>();
for (String number : numbers) {
try {
int firstNum = Integer.parseInt(number);
int secondNum = Integer.parseInt(number);
velocities.add(Velocity.pair(firstNum,secondNum));
我知道我把事情搞砸了,所以我会很高兴听到一些建议。
我想这很简单,我要做的就是用空格拆分数据,就像我已经用逗号拆分数据一样,然后我不知道怎么把这两个数字组合成一个函数。
我的意思是,最后我希望它是:一个速度列表,包含以下值:
Velocity.pair(45,500)
Velocity.pair(46,500)
Velocity.pair(47,500)
谢了。
假设您有一个带有速度信息的数据字符串,可以使用以下代码段:
@Getter
@Setter
@AllArgsConstructor
@ToString
public class Velocity {
int id1;
int id2;
static Velocity pair(int i1, int i2) {
return new Velocity(i1, i2);
}
}
// -------- testing
String velo = "45,500 46,500 47,500";
Arrays.stream(velo.split("\\s+"))
.map(s -> s.split("\\,"))
.map(a -> Velocity.pair(Integer.parseInt(a[0]), Integer.parseInt(a[1])))
.collect(Collectors.toList()) // list of velocity is available here
.forEach(System.out::println);
输出:
Velocity(id1=45, id2=500)
Velocity(id1=46, id2=500)
Velocity(id1=47, id2=500)
假设类velocity
如下所示:
static Velocity {
private int firstNumber;
private int secondNumber;
public Velocity(int firstNumber, int secondNumber) {
super();
this.firstNumber = firstNumber;
this.secondNumber = secondNumber;
}
public int getFirstNumber() {
return firstNumber;
}
public void setFirstNumber(int firstNumber) {
this.firstNumber = firstNumber;
}
public int getSecondNumber() {
return secondNumber;
}
public void setSecondNumber(int secondNumber) {
this.secondNumber = secondNumber;
}
public String toString() {
return "[" + firstNumber + ", " + secondNumber + "]";
}
}
你基本上要一步一步走:
字符串
中删除导入标记ball_velocities:
,将结果拆分
任意数量的空格,然后按逗号拆分
每个结果,int
s速度
,最后velocity
的每个实例添加到列表
例如,可以如下进行:
public static void main(String[] args) throws ParseException {
String data = "ball_velocities:45,500 46,500 47,500";
List<Velocity> velocities = new ArrayList<>();
// remove the intro tag and then split by whitespace(s)
String[] numberPairs = data.replace("ball_velocities:", "").split("\\s+");
// handle each result (which still consists of two numbers separated by a comma
for (String numberPair : numberPairs) {
// that means, split again, this time by comma
String[] numbers = numberPair.split(",");
// parse the results to ints
int firstNum = Integer.parseInt(numbers[0]);
int secondNum = Integer.parseInt(numbers[1]);
// instantiate a new Velocity with the results and add it to the list
velocities.add(new Velocity(firstNum, secondNum));
}
// print the list using the `toString()` method of Velocity
velocities.forEach(System.out::println);
}
此示例将打印
[45, 500]
[46, 500]
[47, 500]
首先,让我们将输入输入到string
数组中:
String[] inputs = data.split(":")[1].split(" ");
本质上,我们首先获得冒号的最右边,然后将其拆分成一个
字符串
项数组,这些项表示您所拥有的输入,其值类似于“45,500”。 让我们创建一个velocity
数组并用项填充它:
Velocity[] velocities new Velocity[inputs.length];
for (int index = 0; index < inputs.length; index++) {
String parts = inputs[index].split(",");
velocities[index] = new Velocity(parts[0], parts[1]);
}