2016-05-25 75 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

相關問題