@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);
}
所以,你需要的是採取一個字符串數組。類似的「1111-222」,「333-444」,「555-666」,並將其存儲到字符串「111」的B陣列,「222」,「333」[...]?它需要成爲一個數組嗎?它可以是一個列表?數組A的字符串總是以這種格式? – Aimnox
@Aimnox是的,這就是我想要 和是它需要一個數組:( ,是的,他們將在該格式不管是什麼,如果它是錯的程序將無法運行 – HSHERU
是否第二陣列需要是一個數組嗎? – Aimnox