2016-05-25 34 views
1

所以,我收到一个字符串数组,我想拆分每个元素并将其保存到一个新的数组中,我面对了很多与该和问题想出了一个非常糟糕的解决方案:String.Split()为一个字符串数组并保存到一个新的数组

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语句,以节省两个要素,我知道这是个不错的办法,但是这就是我能来与:(

+0

所以,你需要的是采取一个字符串数组。类似的“1111-222”,“333-444”,“555-666”,并将其存储到字符串“111”的B阵列,“222”,“333”[...]?它需要成为一个数组吗?它可以是一个列表?数组A的字符串总是以这种格式? – Aimnox

+0

@Aimnox是的,这就是我想要 和是它需要一个数组:( ,是的,他们将在该格式不管是什么,如果它是错的程序将无法运行 – HSHERU

+0

是否第二阵列需要是一个数组吗? – Aimnox

回答

1

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

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); 

(然而,这修剪两个字符串)

+0

如果timeSlots长度发生了变化,所以而不是6它变成了4或8或甚至更多将仍然解决这个问题吗? – HSHERU

+0

它会,你可以看到它的声明数组t为timeSlots的double。'String [] t = new String [2 * timeSlots.length];'它不会工作,但是,如果timeSlots上的字符串包含多次。 – Aimnox

+0

它的工作!非常感谢帮助 – HSHERU

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); 
} 
+0

感谢您的详细解释 – HSHERU

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)); 
} 
+0

谢谢伟大的解决方案,但我想@fabian是更快一点;) – HSHERU

相关问题