有什么解决方案可以从集合中提取每个值吗?

is there any solution to extract each value from a set?

这是我的用户输入。

我有一个简单的命令来请求用户输入。

System.out.println("Enter 5 numbers:");

这就是我将从用户输入中得到的

{5,3,1,2,4}

除了使用 .charAt 提取每个值之外,还有其他方法吗?或者有没有直接识别这种类型输入的函数,我可以简单地使用一种方法来导出值?

这是一个解决方案(在众多解决方案中):

private static final Pattern RE = Pattern.compile(
        "\{([0-9]+(?:,[0-9]+)*)\}");

private static int[] parse(String input) {
    Matcher matcher = RE.matcher(input);
    if (!matcher.matches()) {
        throw new IllegalArgumentException("Invalid input: " + input);
    }
    String[] strings = matcher.group(1).split(",");
    int[] result = new int[strings.length];
    for (int i = 0; i < strings.length; ++i) {
        result[i] = Integer.parseInt(strings[i]);
    }
    return result;
}