简体   繁体   English

将字符串数组分离成多个 arrays

[英]Separate string array into multiple arrays

I have String[] array that contains a string with three data points separated by the character ":" like the following string:我有 String[] 数组,其中包含一个字符串,其中三个数据点由字符“:”分隔,如下所示:

SNSD_OOT:511:127少女时代_OOT:511:127

I need to extract this values from the String[] array.我需要从 String[] 数组中提取这些值。 I tried using statusOxymetry[i] = Arrays.toString(valuesOxymetry[i].split(":"));我尝试使用 statusOxymetry[i] = Arrays.toString(valuesOxymetry[i].split(":")); However I end up with the following data.但是我最终得到了以下数据。 I need to store each element into an array.我需要将每个元素存储到一个数组中。

[SNSD_OOT, 511, 127] [少女时代_OOT, 511, 127]

My goal is to end with 3 arrays, one where anything before the first ":" appears, another one for the data after the first ":", and the final one for the data after the second ":"我的目标是以 3 个 arrays 结尾,其中第一个“:”之前出现任何内容,第一个“:”之后的数据出现另一个,第二个“:”之后的数据出现最后一个

1stArray[0] = SNSD
2ndArray[0] = 511
3rdArray[0] = 127

I need to separate the first, second, and third element into a separate array each.我需要将第一个、第二个和第三个元素分别分成一个单独的数组。 I am feeling a bit confused on how to do this properly.我对如何正确执行此操作感到有些困惑。

                    String[] valuesOxymetry = message.getData().getString("raw"));
                    int total = valuesOxymetry.length - 2;

                    String[] statusOxymetry = new String[total];
                    String[] hrmOxymetry = new String[total];
                    String[] spo2Oxymetry = new String[total];

                    //ignore first two data points (timestamp)
                    for(int i = 2; i < total; i++) {
                        System.out.println("Pulse values: " + valuesOxymetry[i]);
                        statusOxymetry[i] = Arrays.toString(valuesOxymetry[i].split(":"));
                        System.out.println("Only status " + statusOxymetry[i]);

                    }

The String.split method returns an array. String.split方法返回一个数组。 Store that array in a new variable, and use that new variable:将该数组存储在一个新变量中,并使用该新变量:

String[] splitValues = valuesOxymetry[i].split(":")

statusOxymetry[i] = splitValues[0]; // SNSD_OOT
hrmOxymetry[i] = splitValues[1];    // 511
spo2Oxymetry[i] = splitValues[2];   // 127

That said, there may be a cleaner way to do this.也就是说,可能有一种更清洁的方法来做到这一点。 For example you could create a class that represents data records as independent entities, instead of striping the data across "parallel arrays."例如,您可以创建一个 class 将数据记录表示为独立实体,而不是跨“并行 arrays”条带化数据。

It should be:它应该是:

for(int i = 2, j = 0; i < total; i++, j++) {
    System.out.println("Pulse values: " + valuesOxymetry[i]);

    String[] values = valuesOxymetry[i].split(":");
    statusOxymetry[j] = values[0];
    hrmOxymetry   [j] = values[1];
    spo2Oxymetry  [j] = values[2];
}

You can do this:你可以这样做:

public static void main(String[] ar) throws Exception{

    String dataraw = "SNSD_OOT:511:127";

    //take every array from result list
    //print first element
    toListStringArray(dataraw)
        .forEach(
            e -> System.out.println(e[0])
        );

}

public static List<String[]> toListStringArray(String dataraw){

    //split string
    String[] dataArray = dataraw.split(":");

    //list which contains all arrays
    List<String[]> result = new ArrayList<>();

    //take every element from split
    //put into an array which will contains only that element
    //put array in list
    for(String e : dataArray){
        result.add(new String[]{e});
    }

    return result;

}

I don't see why to do this.我不明白为什么要这样做。 I also recommend to look at @Joni answer我还建议查看@Joni 答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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