在java中,根据值拆分字符串列表

osh3o9ms  于 2021-07-14  发布在  Java
关注(0)|答案(2)|浏览(454)

我要做的是拆分一个字符串数组,当字符串中的前6个字符是零(“000000”)或字符串中的所有数字都是零时。限制为6个字符将不是很有活力。
我得到了这个代码,它实现了我想要实现的目标。

import java.util.*;
public class Main
{
    public static void main(String[] args) {
            ArrayList<String> unsplitted = new ArrayList<String>();

            unsplitted.add("000000: this_should_go_into_first_array");
            unsplitted.add("000234: something1");
            unsplitted.add("0000ff: something2");
            unsplitted.add("000111: something3");
            unsplitted.add("000051: something4");
            unsplitted.add("007543: something5");
            unsplitted.add("000000: and_this_should_go_into_second_array");
            unsplitted.add("005612: something7");
            unsplitted.add("005712: something8");

            System.out.println("Unsplitted list: "+ unsplitted);

            List<String> arrlist1 = unsplitted.subList(0, 6);
            List<String> arrlist2 = unsplitted.subList(6, unsplitted.size());

            System.out.println("Sublist of arrlist1: "+ arrlist1);
            System.out.println("Sublist of arrlist2: "+ arrlist2);
    }
}

打印出想要的结果

Sublist of arrlist1: [000000: this_should_go_into_first_array, 000234: something1, 0000ff: something2, 000111: something3, 000051: something4, 007543: somethi
ng5]                                                                                                                                                          
Sublist of arrlist2: [000000: and_this_should_go_into_second_array, 005612: something7, 005712: something8]

但是,我事先不知道零的索引,因此如何通过动态查找零来获得相同的结果?

zz2j4svz

zz2j4svz1#

每次检测到000000字符串时,只需在数组中迭代并创建“bucket”:

ArrayList<String> unsplitted = new ArrayList<String>();

    unsplitted.add("000000: this_should_go_into_first_array");
    unsplitted.add("000234: something1");
    unsplitted.add("0000ff: something2");
    unsplitted.add("000111: something3");
    unsplitted.add("000051: something4");
    unsplitted.add("007543: something5");
    unsplitted.add("000000: and_this_should_go_into_second_array");
    unsplitted.add("005612: something7");
    unsplitted.add("005712: something8");

    List<List<String>> results = new ArrayList<>();
    unsplitted.forEach(w -> {
        if(w.startsWith("000000") || results.isEmpty()) {
            // no bucket or detect 000000
            List<String> bucket = new ArrayList<>();
            bucket.add(w);
            results.add(bucket);
        }
        else {
            // not contains 00000 put the value in the last bucket
            results.get(results.size() - 1).add(w);
        }
    });

    results.forEach(w -> {
        System.out.println("Sublist " + w);
    });

这是你期望的结果吗?
结果是:

Sublist [000000: this_should_go_into_first_array, 000234: something1, 0000ff: something2, 000111: something3, 000051: something4, 007543: something5]
Sublist [000000: and_this_should_go_into_second_array, 005612: something7, 005712: something8]
to94eoyn

to94eoyn2#

这个问题很有趣。有不同的方法来实现这一点,但我将向您展示一个解决方案,它可以应用于第一部分的任何长度,我们可以将其视为关键。
正如您在介绍中所说的,如果支票仅限于6个字符,那么它就不是动态的。基于此,作为一个示例,您可以将字符“:”的位置作为引用,并在数组的元素之间应用分区。
以下是我提出的解决方案:

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main
{
    public static void main(String[] args) {
        ArrayList<String> unsplitted = new ArrayList<String>();

        unsplitted.add("000000: this_should_go_into_first_array");
        unsplitted.add("000234: something1");
        unsplitted.add("0000ff: something2");
        unsplitted.add("000111: something3");
        unsplitted.add("000051: something4");
        unsplitted.add("007543: something5");
        unsplitted.add("000000: and_this_should_go_into_second_array");
        unsplitted.add("005612: something7");
        unsplitted.add("005712: something8");

        System.out.println("Non-split list: "+ unsplitted);

        Predicate<String> filter = (String s) -> {
            int indexOfCol = s.indexOf(":");

            return s.substring(0, indexOfCol).equals("0".repeat(indexOfCol));
        };

        Map<Boolean, List<String>> splitMap = unsplitted.stream()
                                                        .collect(Collectors.partitioningBy(filter));

        List<String> arrayZeroStart = splitMap.get(true);
        List<String> arrayNonZeroStart = splitMap.get(false);

        System.out.println("Sublist of arrayZeroStart: "+ arrayZeroStart);
        System.out.println("Sublist of arrayWithout: "+ arrayNonZeroStart);
    }
}

下面是输出:

Non-split list: [000000: this_should_go_into_first_array, 000234: something1, 0000ff: 

something2, 000111: something3, 000051: something4, 007543: something5, 000000: and_this_should_go_into_second_array, 005612: something7, 005712: something8]
Sublist of arrayZeroStart: [000000: this_should_go_into_first_array, 000000: and_this_should_go_into_second_array]
Sublist of arrayWithout: [000234: something1, 0000ff: something2, 000111: something3, 000051: something4, 007543: something5, 005612: something7, 005712: something8]

相关问题