2016-05-25 4 views
1

So empfange ich ein Array von String und ich möchte jedes Element teilen und speichern Sie es in ein neues Array und ich habe viel konfrontiert mit den Problemen und kam mit einer wirklich schlechten Lösung:String.Split() für ein Array von String und Speichern in ein neues Array

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

Wie Sie sehen, ich habe eine if-Anweisung, um zwei Elemente zu speichern, ich weiß, dass es ein schlechter Ansatz ist, aber das ist alles, was ich war in der Lage zu kommen mit :(

+0

Also, was Sie brauchen, ist ein Array A von Zeichenketten wie „1111-222“ zu nehmen, „333-444“, „555-666“ und speichern Sie es in ein Array B von Zeichenfolge „111“ , "222", "333" [...]? Muss es ein Array sein? Kann es eine Liste sein? Die Zeichenfolge des Arrays A ist immer in diesem Format? ja – Aimnox

+0

@Aimnox das ist, was ich will und ja es muss ein Array sein :( und ja sie in diesem Format egal sein wird, was ist, wenn es falsch war, wird das Programm nicht – HSHERU

+0

Hat das zweite Array laufen müssen Seien Sie ein Array? Kann es eine Liste sein? – Aimnox

Antwort

1

Sie können den Index in der Ergebnismatrix aus dem Index in dem Eingangsfeld berechnen:

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

oder benutzen Streams:

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

(diese beiden Saiten jedoch trimmt)

+0

was ist, wenn die TimeSlots Länge geändert, so anstelle von 6 wurde es 4 oder 8 oder noch mehr wird das immer noch das Problem lösen? – HSHERU

+0

Es wird, wie Sie sehen, das Array T als das Double von timeSlots deklarieren. 'String [] t = new String [2 * timeSlots.length];' Es wird nicht funktionieren, wenn die Strings auf timeSlots mehr als einmal enthalten sind – Aimnox

+0

Es hat funktioniert! Vielen Dank für die Hilfe – 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

Danke für die ausführliche Erklärung – HSHERU

0

Wenn die Struktur Ihres Arrays immer die gleiche ist, können Sie zuerst ein die Elemente Ihrer Array beitreten string und split wieder nach jeder Stunde. Beispiel:

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

danke tolle Lösung, aber ich denke, @fabian war ein bisschen schneller;) – HSHERU

Verwandte Themen