繁体   English   中英

拆分成对数字的字符串,然后在 java 中拆分每个数字

[英]Splitting string of pairs of numbers, then splitting each one of them in java

假设我有以下行:

ball_velocities:45,500 46,500 47,500

我想:

  • 将对彼此分开
  • 将这对本身和其中的数字彼此分开使用我已经拥有的 function 中的这两个数字
 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 :

  1. 从要拆分的String中删除引入标签ball_velocities:
  2. 用任意数量的空格split结果,然后
  3. 用逗号split每个结果,
  4. 将结果解析为int s
  5. 用解析的结果实例化一个Velocity ,最后
  6. 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(" ");

本质上,我们首先获取冒号的最右侧部分,并将其splitString项数组,这些项表示您的输入,其值类似于“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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM