Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.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 这个程序打印的是错误的reutls_Java_Recursion - Fatal编程技术网

Java 这个程序打印的是错误的reutls

Java 这个程序打印的是错误的reutls,java,recursion,Java,Recursion,我想做的是: 我有一个值列表,我想分别列出这三个值。例如: 我有: "one", "two", "three", "four", "five" 我想打印以下内容: one two three one two four one four five two three four two three five and so on 你说得对,我要每三个值(不是按顺序偏离航线) 我所做的是: import java.util.*; public class Solution { public

我想做的是:

我有一个值列表,我想分别列出这三个值。例如:

我有:

"one", "two", "three", "four", "five"
我想打印以下内容:

one two three
one two four
one four five
two three four
two three five
and so on
你说得对,我要每三个值(不是按顺序偏离航线)

我所做的是:

import java.util.*;

public class Solution {
    public static void main(String args[]) {
        String[] people = new String[] { "one", "two", "three", "four",
                "five" };
        Solution s = new Solution();
        s.solve(people, 3, new LinkedList<>(), 0);
    }

    public void solve(String[] people, int n, List<String> data, int i) {
        if (data.size() == n) {
            System.out.println(data.toString());

        } else if (i < people.length) {
            String value = people[i];
            solve(people, n, data, i + 1);
            data.add(value);
            solve(people, n, data, i + 1);
        }
    }
}
import java.util.*;
公共类解决方案{
公共静态void main(字符串参数[]){
字符串[]人=新字符串[]{“一”、“二”、“三”、“四”,
“五”};
溶液s=新溶液();
s、 求解(人物,3,新LinkedList(),0);
}
公共void solve(字符串[]人,整数n,列表数据,整数i){
if(data.size()==n){
System.out.println(data.toString());
}else if(i
您可以运行它:

我的问题是它打印:

[五,四,五]

这显然是错误的,它如何打印两个相同的值?在我的代码中,我只添加一次值,以后不再添加

你能帮忙吗

它如何打印两个相同的值

原因是您在代码中设置的条件:

else if (i < people.length) 
else if(i
要让您了解执行框架,请执行以下操作:

solve(people, n, data, i + 1); 

// (1) this recurses until i = 4(which is less than the length=5)
// for the next iteration the condition i<people.length wouldn't be satisfied

// (2) hence this is called
data.add(value);  // data now has "five"

// since when the previous recursion ended i=4 (callee)
solve(people, n, data, i + 1); 
// (3) this recurses again and finds the same condition of not being able to recurse more
// hence data gets value = "four"
solve(人,n,数据,i+1);
//(1)该循环直到i=4(小于长度=5)

//对于下一次迭代,条件i@PM77-1我不是问怎么做,我是问代码中的错误。@Paolo RLang在打印语句中放一个调试点,然后测试一次。@nullpointer为什么值“5”会出现两次?对meLearn来说,调试你的代码是很奇怪的。一步一步地运行它,您将看到。请查看这个可爱的博客以获取帮助。
public void solve(String[] people, int n, List<String> data, int i) {
    if (data.size() == n) {
        System.out.println(data.toString());
        return;
    }

    if (i == people.length) {
        return;
    }

    data.add(people[i]);
    solve(people, n, data, i + 1);
    data.remove(people[i]);
    solve(people, n, data, i + 1);
}
[one, two, three]
[one, two, four]
[one, two, five]
[one, three, four]
[one, three, five]
[one, four, five]
[two, three, four]
[two, three, five]
[two, four, five]
[three, four, five]