[英]Splitting string of pairs of numbers, then splitting each one of them in java
假设我有以下行:
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));
我知道我搞砸了,所以我很高兴听到一些建议。
我认为这很简单,我要做的就是用空格分割数据,因为我已经用逗号再次分割了数据,然后我不知道如何将这两个数字组合成一个 function。
我的意思是最后我希望它是:包含以下值的速度列表:
Velocity.pair(45,500)
Velocity.pair(46,500)
Velocity.pair(47,500)
谢谢。
假设 class Velocity
如下所示:
class 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 + "]";
}
}
你基本上必须一步一步 go :
String
中删除引入标签ball_velocities:
split
结果,然后split
每个结果,int
sVelocity
,最后Velocity
的每个实例添加到List<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]
假设您有一串包含速度信息的数据,您可以使用以下代码段:
@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 input = "ball_velocities:45,500 46,500 47,500";
String velo = input.replaceAll("ball_velocities\\s*\\:\\s*", ""); //remove prefix containing optional whitespaces
// 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);
output:
Velocity(id1=45, id2=500)
Velocity(id1=46, id2=500)
Velocity(id1=47, id2=500)
首先让我们将输入放入一个String
数组中:
String[] inputs = data.split(":")[1].split(" ");
本质上,我们首先获取冒号的最右侧部分,并将其split
为String
项数组,这些项表示您的输入,其值类似于“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]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.