0

假设我有以下行:

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));

我知道我搞砸了,所以我很高兴听到一些建议。

我认为这很简单,我要做的就是将 data.split 用空格分割,因为我已经用逗号再次分割了数据,然后我不知道如何将这两个数字组合成一个函数。

我的意思是最后我希望它是:包含以下值的速度列表:

Velocity.pair(45,500)
Velocity.pair(46,500)
Velocity.pair(47,500)

谢谢。

4

3 回答 3

1

假设类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 + "]";
    }
}

你基本上必须一步一步地走:

  1. ball_velocities:String要拆分的标签中删除引入标签,
  2. split结果由任意数量的空格,然后
  3. split逗号的每个结果,
  4. 将结果解析为ints
  5. 用解析结果实例化 aVelocity最后
  6. 将每个实例添加VelocityList<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]
于 2020-06-19T14:32:24.363 回答
0

首先让我们将输入放入一个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]);
}
于 2020-06-19T14:50:55.540 回答
0

假设您有一串包含速度信息的数据,您可以使用以下代码段:

@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);

输出:

Velocity(id1=45, id2=500)
Velocity(id1=46, id2=500)
Velocity(id1=47, id2=500)
于 2020-06-19T14:26:55.750 回答