@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