java如何根据元素将重复元素和不同值的字符串的一部分排序到不同的数组中?
我目前正在尝试根据元素(如freq、mag等)将以下字符串转换为几个数组。该字符串最初是json格式的,但使用Gson,它被转换为我正在使用的另一个类的对象,然后转换为字符串,以便更容易排序(对我来说)我正在寻找关于如何将其转换为字符串数组的建议,或者是否有其他更简单的方法来准备多个数组
[freq: 17.97 mag: 8.22 bw: 0.44 snr: 2.47 med: 3.33, freq: 9.96 mag: 4.06 bw: 0.41 snr: 2.31 med: 1.76, freq: 23.05 mag: 0.92 bw: 0.41 snr: 2.24 med: 0.41, freq: 12.99 mag: 1.07 bw: 0.38 snr: 2.19 med: 0.49, freq: 7.18 mag: 18.54 bw: 0.47 snr: 2.15 med: 8.61]
理想情况下,我最终会得到如下结果:
frequencyArray[0] = 17.97
frequencyArray[1] = 9.96
frequencyArray[x] = [x]
等等
# 1 楼答案
地图是你的朋友
输出:
# 2 楼答案
你可以试着把它放到这样的地图上
我假设您的格式非常规则,两个空格分隔两个键值对,逗号分隔数据行
这个的输出
是
假设输入是这样定义的(忽略换行符,它们只是为了格式化,因为代码不换行)
虽然键的顺序没有保留(如果需要,您可以使用
LinkedHashMap
),但内部数字的顺序是使用流的另一种方法:
输出
# 3 楼答案
您可以使用
Map<Integer, Double[]>
存储所有数组。首先按,
拆分,然后按拆分。必须进行清洁和过滤,并使用索引来知道将阀门放置在何处
之后,您可以通过索引获取阵列或打印整个地图:
# 4 楼答案
一个
Map
的List
更适合这个问题输出:
我已经清理了一些字符串,比如在一些值之后删除了额外的
","