Java String.Split()用于字符串数组并保存到新数组中

Java String.Split()用于字符串数组并保存到新数组中,java,arrays,string,split,arrayofstring,Java,Arrays,String,Split,Arrayofstring,因此,我收到一个字符串数组,我想将每个元素拆分并保存到一个新数组中,我遇到了很多问题,并提出了一个非常糟糕的解决方案: 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

因此,我收到一个字符串数组,我想将每个元素拆分并保存到一个新数组中,我遇到了很多问题,并提出了一个非常糟糕的解决方案:

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++;
}
String[]时隙={“13:00:00-14:00:00”,“15:00:00-16:00:00”,“17:00:00-18:00:00”};
字符串[]t=新字符串[6];
字符串温度[]=新字符串[6];
int j=1;
对于(int i=0;i<3;i++){
temp=时隙[i]。拆分(“\\-”;
如果(j==1){
t[0]=温度[0];
t[1]=温度[1]。修剪();
}
else如果(j==2){
t[2]=温度[0];
t[3]=温度[1]。修剪();
}
否则{
t[4]=温度[0];
t[5]=温度[1]。修剪();
}
j++;
}

正如您所看到的,我必须创建一个if语句来保存两个元素,我知道这是一个糟糕的方法,但这就是我所能得到的:(

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

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();
}
(但这会修剪两个字符串)

@Test
public void splitTimeSlotsToArray(){
字符串[]时隙={“13:00:00-14:00:00”,“15:00:00-16:00:00”,“17:00:00-18:00:00”};
//我们已经知道每个范围(或插槽)有多少次
//其中指定了两个时间段。所以它是时间段长度乘以2。
String[]times=新字符串[timeslot.length*2];
对于(int i=0;i
如果数组的结构始终相同,则可以先将数组的元素连接到字符串,然后在每小时后再次拆分。例如:

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

因此,您需要的是获取一个字符串数组A,如“1111-222”、“333-444”、“555-666”,然后将其存储到字符串“111”、“222”、“333”[…]的数组B中?它需要是数组吗?它可以是列表吗?数组A的字符串都是这种格式吗?@Aimnox是的,这是我想要的,是的,它需要是数组:(是的,它们将是这种格式,无论它是错误的,程序将不会运行第二个数组需要是数组吗?它可以是列表吗?@Aimnox抱歉,检查新编辑的注释在这种情况下,@fabian解决方案似乎就是你所需要的了。如果时隙长度改变了,而不是6,那么它将变成4或8或更多,这还会吗解决问题?它会的,正如你所看到的,它将数组t声明为时隙的两倍。
String[]t=new String[2*timeSlots.length];
但是,如果时隙上的字符串包含多个时间,它将不起作用。感谢伟大的解决方案,但我想@fabian的速度快了一点;)谢谢你的详细解释
@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);
}
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));
}