Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何显示数组中某些值的组合?_Java_Algorithm_Math - Fatal编程技术网

Java 如何显示数组中某些值的组合?

Java 如何显示数组中某些值的组合?,java,algorithm,math,Java,Algorithm,Math,例如,我有一个数组[“Sam”、“Mary”、“John”] 我想显示三选二的组合。 结果应该是: [Sam, Mary] [Sam, John] [Mary, John] 我做了很多研究,但仍然不知道怎么做。 当然,此示例仅包含3个人。 事实上,总人数将更大,例如15人 以下是我的发现: 其中一些仅显示nCr的值,而不给出组合。简单的递归函数,用于打印给定字符串数组(名为数组)的组合(nCr): String[]数组={“Sam”、“Mary”、“John”}; 公共无效函数(int计

例如,我有一个数组
[“Sam”、“Mary”、“John”]

我想显示三选二的组合。
结果应该是:

[Sam, Mary]
[Sam, John]
[Mary, John] 
我做了很多研究,但仍然不知道怎么做。
当然,此示例仅包含3个人。
事实上,总人数将更大,例如15人

以下是我的发现:


其中一些仅显示nCr的值,而不给出组合。

简单的递归函数,用于打印给定字符串数组(名为
数组
)的组合(nCr):

String[]数组={“Sam”、“Mary”、“John”};
公共无效函数(int计数器、字符串组合、int r){
如果(r==0){
系统输出打印LN(comb_Str);
}否则{
用于(;计数器
使用
函数(0,“,#r值#)调用


r值应该是简单的递归函数,用于打印给定字符串数组(名为
数组
)的组合(nCr):

String[]数组={“Sam”、“Mary”、“John”};
公共无效函数(int计数器、字符串组合、int r){
如果(r==0){
系统输出打印LN(comb_Str);
}否则{
用于(;计数器
使用
函数(0,“,#r值#)调用


r值应该是以下是一些伪代码,让您开始使用递归解决方案。列表将比字符串数组更容易使用,因为您可以轻松更改其大小。另外,一旦你得到了你的组合,你可以迭代它们来显示它们。然而,尽管这是一个需要考虑的好问题,但组合的数量很快就会失控,因此,如果您处理的结果超过少数,那么将它们全部显示给用户将是一个坏主意

/**
 * @param list The list to create all combos for
 * @param comboSize The size of the combo lists to build (e.g. 2 for 2 items combos)
 * @param startingIndex The starting index to consider (used mainly for recursion).  Set to 0 to consider all items.
 */
getAllCombos(list, comboSize, startingIndex){
    allCombos;

    itemsToConsider = list.length - startingIndex;
    if(itemsToConsider >= comboSize){
        allCombos = getAllCombos(list, comboSize, startingIndex + 1);

        entry = list[startingIndex];
        if(comboSize == 1){
            singleList;
            singleList.add(entry);
            allCombos.add(singleList);
        } else {
            subListCombos = getAllCombos(list, comboSize - 1, i+1);
            for(int i = 0; i < subListCombos.length; i++){
                subListCombo = subListCombos[i];
                subListCombo.add(entry);
                allCombos.add(subListCombo);
            }
        }
    }

    return allCombos;
}
/**
*@param列出要为其创建所有组合的列表
*@param comboSize要构建的组合列表的大小(例如,2个项目组合为2个)
*@ PARAM启动索引:要考虑的起始索引(主要用于递归)。设置为0考虑所有项目。
*/
getAllCombos(列表、comboSize、开始索引){
所有组合;
itemstoconsive=list.length-startingIndex;
如果(itemstoconsumer>=组合大小){
allCombos=getAllCombos(列表、comboSize、startingIndex+1);
条目=列表[起始索引];
如果(comboSize==1){
单一名单;
singleList.add(条目);
allCombos.add(singleList);
}否则{
subListCombos=getAllCombos(列表,comboSize-1,i+1);
for(int i=0;i
以下是一些伪代码,让您开始使用递归解决方案。列表将比字符串数组更容易使用,因为您可以轻松更改其大小。另外,一旦你得到了你的组合,你可以迭代它们来显示它们。然而,尽管这是一个需要考虑的好问题,但组合的数量很快就会失控,因此,如果您处理的结果超过少数,那么将它们全部显示给用户将是一个坏主意

/**
 * @param list The list to create all combos for
 * @param comboSize The size of the combo lists to build (e.g. 2 for 2 items combos)
 * @param startingIndex The starting index to consider (used mainly for recursion).  Set to 0 to consider all items.
 */
getAllCombos(list, comboSize, startingIndex){
    allCombos;

    itemsToConsider = list.length - startingIndex;
    if(itemsToConsider >= comboSize){
        allCombos = getAllCombos(list, comboSize, startingIndex + 1);

        entry = list[startingIndex];
        if(comboSize == 1){
            singleList;
            singleList.add(entry);
            allCombos.add(singleList);
        } else {
            subListCombos = getAllCombos(list, comboSize - 1, i+1);
            for(int i = 0; i < subListCombos.length; i++){
                subListCombo = subListCombos[i];
                subListCombo.add(entry);
                allCombos.add(subListCombo);
            }
        }
    }

    return allCombos;
}
/**
*@param列出要为其创建所有组合的列表
*@param comboSize要构建的组合列表的大小(例如,2个项目组合为2个)
*@ PARAM启动索引:要考虑的起始索引(主要用于递归)。设置为0考虑所有项目。
*/
getAllCombos(列表、comboSize、开始索引){
所有组合;
itemstoconsive=list.length-startingIndex;
如果(itemstoconsumer>=组合大小){
allCombos=getAllCombos(列表、comboSize、startingIndex+1);
条目=列表[起始索引];
如果(comboSize==1){
单一名单;
singleList.add(条目);
allCombos.add(singleList);
}否则{
subListCombos=getAllCombos(列表,comboSize-1,i+1);
for(int i=0;i
公共静态整数宽度;
公共静态void main(字符串[]args){
字符串[]数组={“一”、“二”、“三”、“四”、“五”};
宽度=3;
列表=新的ArrayList();
for(int i=0;i
公共静态整数宽度;
公共静态void main(字符串[]args){
字符串[]数组={“一”、“二”、“三”、“四”、“五”};
宽度=3;
列表=新的ArrayList();
for(int i=0;i    public static int width;

    public static void main(String [] args){

        String[] array = {"one", "two", "three", "four", "five"};

        width = 3;

        List<String> list = new ArrayList<String>();

        for (int i = 0; i < array.length; i++){
            method(array, list, i, 1, "[" + array[i]);
        }

        System.out.println(list);
    }


    public static void method(String[] array, List<String> list, int i, int depth, String string){

        if (depth == width){
            list.add(string + "]");
            return;
        }

        for (int j = i+1; j < array.length; j++){
            method(array, list, j, depth+1, string + ", " + array[j]);
        }
    }
int num = 2; //Number of elements per combination

for(int i=0; i <= (array.length - num); i++) {
    String comb = "[" + array[i];
    comb += getComb(i,num);
    comb += "]";
    println(comb);
}

String getComb(int i, int num) {
    int counter = 1;
    String s = "";

    while(counter < num) {
        s += ", " + array[i+counter];
        counter++;
    }

    return s;
}