如何将两个或多个字符串数组列表的排列和组合创建为一个组合列表

时间:2019-02-14 08:40:12

标签: java string combinations permutation

我想获取多个字符串arraylist的所有组合

public class NumberTextWatcherForThousand implements TextWatcher {

EditText editText;


public NumberTextWatcherForThousand(EditText editText) {
    this.editText = editText;


}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable view) {
String s = null;
try {
    // The comma in the format specifier does the trick
    s = String.format("%,d", Long.parseLong(view.toString()));
    edittext.settext(s);
} catch (NumberFormatException e) {
}

输入:

String arr1[]={"red","green"}
String arr2[]={"underline"}
ArrayList a=get_Combination(arr1,arr2)

输出: 组合列表应如下所示

array 1=["red","green","blue"].
array 2=["bold"].
array 3=["underline"].`

1 个答案:

答案 0 :(得分:0)

这是您想要的吗?

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Combin {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final String[][] array = new String[][] {
            new String[] {"red","green","blue"}
            , new String[] {"bold"}
            , new String[] {"underline"}
        };

        final List<String> list = combination(Arrays.asList(array));

        list.stream()
        .forEach(
            System.out::println
        )
        ;
    }

    static List<String> combination(
            final List<String[]> array)
    {
        if(array == null || array.size() < 1) {
            return Collections.<String>emptyList();
        }

        if(array.size() == 1) {
            return Arrays.stream(array.get(0))
                    .collect(Collectors.toList())
                    ;
        }

        if(array.size() == 2) {
            List<String> list1 = Arrays.stream(array.get(0))
                                .collect(Collectors.toList())
                                ;

            List<String> list2 = Arrays.stream(array.get(1))
                                .collect(Collectors.toList())
                                ;

            final List<String> list3 = new ArrayList<>();
            list1.stream()
            .forEach(
                s1 -> {
                    list2.stream()
                    .forEach(
                        s2 -> {
                            list3.add(s1 + ", " + s2);
                        }
                    );
                }
            )
            ;

            final List<String> list = new ArrayList<>();
            list.addAll(list1);
            list.addAll(list2);
            list.addAll(list3);

            return list;
        }

        //length > 2
        List<String> list = combination(array.subList(0, 2));

        for(int i=2; i<array.size(); i++) {
            List<String[]> newArrayList = new ArrayList<>();
            newArrayList.add(list.toArray(new String[list.size()]));
            newArrayList.add(array.get(i));

            list = combination(newArrayList);
        }

        return list;

    }
}

结果:

red
green
blue
bold
red, bold
green, bold
blue, bold
underline
red, underline
green, underline
blue, underline
bold, underline
red, bold, underline
green, bold, underline
blue, bold, underline