Java递归如何
我有一个只存储两个整数的类:Java递归如何,java,algorithm,recursion,Java,Algorithm,Recursion,我有一个只存储两个整数的类: public class Item { private int from; private int to; public Item(int from, int to) { this.from = from; this.to = to; } getters and setters } 在运行时,我将创建这些项目的列表。该列表将包含1到21个值。然后我需要遍历这些值并进行一些处理。例如:
public class Item
{
private int from;
private int to;
public Item(int from, int to)
{
this.from = from;
this.to = to;
}
getters and setters
}
在运行时,我将创建这些项目的列表。该列表将包含1到21个值。然后我需要遍历这些值并进行一些处理。例如:
public void validate()
{
List<Item> items = new ArrayList<Item>();
items.add(new Item(1,3));
items.add(new Item(11,13));
}
或:
然后我需要循环这些值。显然,我不知道要循环多少层,所以我不能硬编码一个循环。如果可以,循环将如下所示:
public void loop()
{
String str;
for(int i = 1; i < 3; i++)
{
str = "-" + i;
for(int j = 11; j < 13; j++)
{
str = str + "-" + j;
do stuff with str;
}
}
}
我需要在这里测试的输出是:
-1-11
-1-12
-1-13
-2-11
-2-12
-2-13
-3-11
-3-12
-3-13
第二个例子是:
public void loop()
{
String str;
for(int i = 1; i < 3; i++)
{
str = "-" + i;
for(int j = 11; j < 13; j++)
{
str = str + "-" + j;
for(k = 21; k < 32; k++)
{
str = str + "-" + k;
do stuff with str;
}
}
}
}
我需要在这里测试的输出是:
-1-11-21
-1-11-22
-1-11-23
-1-12-21
-1-12-22
...
-2-11-21
-2-11-22
...
-3-13-21
因为我不知道有多少个循环,我想我需要使用递归。
然而,我不知道如何在递归的内部循环中使用str,我开始认为这是不可能的
这就是我在这里尝试的:
private void recursiveExplore(List<Item> items, int depth)
{
if (depth == 0) return;
for (Item item : items)
{
for (int i = item.getFromParameterId(); i < item.getToParameterId(); i++)
{
do stuff here?
}
recursiveExplore(items.subList(items.size() - depth, items.size()), depth - 1);
}
}
public void validate()
{
recursiveExplore(items, items.size());
}
谢谢你的帮助
ETA SSCCE:
import java.util.ArrayList;
import java.util.List;
public class MyTest
{
private class Item
{
private int from;
private int to;
public Item(int from, int to)
{
this.from = from;
this.to = to;
}
public int getFrom()
{
return from;
}
public int getTo()
{
return to;
}
}
public void validate2()
{
List<Item> items = new ArrayList<Item>();
items.add(new Item(1, 3));
items.add(new Item(11, 13));
String str;
for (int i = items.get(0).getFrom(); i <= items.get(0).getTo(); i++)
{
for (int j = items.get(1).getFrom(); j <= items.get(1).getTo(); j++)
{
str = "-" + i + "-" + j;
System.out.println(str);
}
}
}
public void validate3()
{
List<Item> items = new ArrayList<Item>();
items.add(new Item(1, 3));
items.add(new Item(11, 13));
items.add(new Item(21, 23));
String str;
for (int i = items.get(0).getFrom(); i <= items.get(0).getTo(); i++)
{
for (int j = items.get(1).getFrom(); j <= items.get(1).getTo(); j++)
{
for (int k = items.get(2).getFrom(); k <= items.get(2).getTo(); k++)
{
str = "-" + i + "-" + j + "-" + k;
System.out.println(str);
}
}
}
}
public static void main(String[] args)
{
MyTest test = new MyTest();
test.validate2();
test.validate3();
}
}
也许你想要这样的东西
public class Item(){}
public class ListItem extends Item(){} //here you have a List of items
public class ObjectItem extends Item() {} //here you have only one item
因此,您可以使用instanceof运算符进行递归搜索。检查循环后,我设想您最终需要使用-来更新str变量。如果是这样,那么您就进入了编写轨道
private void recursiveExplore(List<Item> items, int depth, int str) {
if (depth == 0) return;
for (Item item : items) {
for (int i = item.getFromParameterId(); i < item.getToParameterId(); i++) {
str += "-" + i;
}
// Just need to concatenate the output of nested call
str += recursiveExplore(items.subList(items.size() - depth, items.size()), depth - 1, str);
}
return str;
}
public void validate() {
String finalStr = recursiveExplore(items, items.size(),"");
}
未尝试过,但可能需要稍作调整。在同事的帮助下,我已经完成了所需的代码,请参见下文。它不太完整,但主要结构在那里。 谢谢你的帮助,没有你的帮助我也做不到
public void validateFiles()
{
List<String> outputList = new ArrayList<String>();
recursiveCompile(items, 0, null, outputList);
}
private void recursiveCompile(ArrayList<Item> items, int itemIndex,
String compilation, List<String> outputList)
{
if (itemIndex < items.size())
{
/* Get the item from the list. */
Item item = items.get(itemIndex);
/* Prepare the variables for the descent. */
itemIndex++;
if (compilation == null) compilation = "";
/* Descend for each value in the from/to range. */
for (int i = item.getFromParameterId(); i <= item.getToParameterId(); i++)
{
String s = compilation + "-" + item.getNextValue();
recursiveCompile(items, itemIndex, s, outputList);
}
}
else
{
/*
* The bottom has been reached. Add the compiled string to the
* output list and ascend (return).
*/
outputList.add(compilation);
return;
}
}
您使用的Java版本是JDK1.4、1.5、6还是7?有很多错误。请提供一个例子。它教会了你很多,也帮助了我们帮助你。我认为你走了一条极其复杂的道路来解决这个问题。你能告诉我你到底想要实现什么吗?你到底想要实现什么?如果你能通过递归实现,那么你也可以通过迭代实现。那太棒了,谢谢。我当时正朝那个方向走,但我还不知道回报会到哪里去。我现在要修改我的代码。
public void validateFiles()
{
List<String> outputList = new ArrayList<String>();
recursiveCompile(items, 0, null, outputList);
}
private void recursiveCompile(ArrayList<Item> items, int itemIndex,
String compilation, List<String> outputList)
{
if (itemIndex < items.size())
{
/* Get the item from the list. */
Item item = items.get(itemIndex);
/* Prepare the variables for the descent. */
itemIndex++;
if (compilation == null) compilation = "";
/* Descend for each value in the from/to range. */
for (int i = item.getFromParameterId(); i <= item.getToParameterId(); i++)
{
String s = compilation + "-" + item.getNextValue();
recursiveCompile(items, itemIndex, s, outputList);
}
}
else
{
/*
* The bottom has been reached. Add the compiled string to the
* output list and ascend (return).
*/
outputList.add(compilation);
return;
}
}