1

所以,我收到一个字符串数组,我想拆分每个元素并将其保存到一个新数组中,我遇到了很多问题,并提出了一个非常糟糕的解决方案:

String[] timeSlots = { "13:00:00 - 14:00:00", "15:00:00 - 16:00:00","17:00:00 - 18:00:00" };
String[] t = new String[6];
String temp[] = new String[6];
int j = 1;
for (int i = 0; i < 3; i++) {
    temp = timeSlots[i].split("\\-");
    if(j == 1){
        t[0] = temp[0];
        t[1] = temp[1].trim();
    }
    else if(j == 2){
        t[2] = temp[0];
        t[3] = temp[1].trim();
    }
    else{
        t[4] = temp[0];
        t[5] = temp[1].trim();
    }
    j++;
}

如您所见,我必须创建一个 if 语句来保存两个元素,我知道这是一种不好的方法,但这就是我所能想到的 :(

4

3 回答 3

2

您可以根据输入数组中的索引计算结果数组中的索引:

String[] t = new String[2*timeSlots.length];

for (int i = 0; i < timeSlots.length; i++) {
    String[] temp = timeSlots[i].split("\\-");
    t[2*i] = temp[0].trim();
    t[2*i+1] = temp[1].trim();
}

或使用流:

t = Arrays.stream(timeSlots).flatMap(slot -> Arrays.stream(slot.split("\\-")).map(String::trim)).toArray(String[]::new);

(但这会修剪两个字符串)

于 2016-05-25T08:04:03.640 回答
0
@Test
public void splitTimeSlotsToArray() {
    String[] timeSlots = { "13:00:00 - 14:00:00", "15:00:00 - 16:00:00","17:00:00 - 18:00:00" };

    // We already know how many times there are, each range (or slot)
    // has two times specified in it. So it's the length of timeSlots times 2.
    String[] times = new String[timeSlots.length*2];

    for (int i = 0; i < timeSlots.length; i++) {
        String timeSlotParts[] = timeSlots[i].split(" - ");
        times[i*2] = timeSlotParts[0];
        times[i*2 + 1] = timeSlotParts[1];
    }

    assertEquals(Arrays.asList(
        "13:00:00", "14:00:00", "15:00:00", "16:00:00", "17:00:00", "18:00:00"
    ), Arrays.asList(times));
}

// This is a more preferable option in terms of readability and
// idiomatics in Java, however it also uses Java collections which you
// may not be using in your class
@Test
public void splitTimeSlotsToList() {
    String[] timeSlots = { "13:00:00 - 14:00:00", "15:00:00 - 16:00:00","17:00:00 - 18:00:00" };
    Collection<String> times = new ArrayList<>();

    // Go over each time slot
    for (String timeSlot : timeSlots) {
        // Go over each time in each time slot
        for (String time : timeSlot.split(" - ")) {
            // Add that time to the times collection
            times.add(time);
        }
    }
    // you can convert the Collection to an array too:
    // String[] timesArray = times.toArray(new String[timeStamps.size()]);

    assertEquals(Arrays.asList(
        "13:00:00", "14:00:00", "15:00:00", "16:00:00", "17:00:00", "18:00:00"
    ), times);
}
于 2016-05-25T08:04:22.503 回答
0

如果您的数组的结构始终相同,您可以先将数组的元素连接到一个字符串,然后在每个小时后再次拆分。例子:

public static void main(String a[]){
    String[] timeSlots = { "13:00:00 - 14:00:00", "15:00:00 - 16:00:00","17:00:00 - 18:00:00" };
    String joined = String.join(" - ", timeSlots);// gives you a string like this "13:00:00 - 14:00:00 - 15:00:00 - 16:00:00 - 17:00:00 - 18:00:00"
    String [] newArray = joined.split(" - ");
    System.out.println(Arrays.toString(newArray));
}
于 2016-05-25T08:10:31.637 回答