【问题标题】:String.Split() for a array of string and saving into a new arrayString.Split() 用于字符串数组并保存到新数组中
【发布时间】:2016-05-25 07:51:34
【问题描述】:

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

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 语句来保存两个元素,我知道这是一种不好的方法,但这就是我所能想到的 :(

【问题讨论】:

  • 所以你需要的是把一个像“1111-222”、“333-444”、“555-666”这样的字符串数组A存储到一个字符串“111”的数组B中,“222”,“333”[...]?它需要是一个数组吗?它可以是一个列表吗?数组A的字符串总是那种格式?
  • @Aimnox 是的,这就是我想要的,是的,它需要是一个数组 :( 是的,不管它有什么问题,程序都不会运行,它们都会采用这种格式
  • 第二个数组需要是数组吗?可以是列表吗?
  • @Aimnox 抱歉检查新编辑的评论
  • 在这种情况下,@fabian 解决方案似乎就是您所需要的全部

标签: java arrays string split arrayofstring


【解决方案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);

(不过这会修剪两个字符串)

【讨论】:

  • 如果 timeSlots 的长度改变了,而不是 6 变成了 4 或 8 甚至更多,这仍然可以解决问题吗?
  • 它会的,正如你所看到的,它将数组 t 声明为 timeSlots 的双倍。 String[] t = new String[2*timeSlots.length]; 但是,如果 timeSlots 上的字符串包含超过一次,它将不起作用。
【解决方案2】:
@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);
}

【讨论】:

  • 感谢详细解释
【解决方案3】:

如果您的数组的结构始终相同,您可以先将数组的元素连接到一个字符串,然后在每个小时后再次拆分。示例:

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

【讨论】:

  • 感谢伟大的解决方案,但我猜@fabian 有点快;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 2018-06-26
  • 2020-01-02
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多