Java 如何递归删除未排序列表中小于给定int的所有元素?

Java 如何递归删除未排序列表中小于给定int的所有元素?,java,recursion,arraylist,Java,Recursion,Arraylist,我一直在研究这种递归方法,但似乎无法理解它。我的方法是:我决定创建列表的副本,然后为递归调用删除副本的最后一个元素。如果最后一项(复制列表的最后一项,这是我尝试循环遍历列表的方式)大于给定的int,我也会将其从实际列表中删除 public MyList<Integer> reduceMyList(MyList<Integer> m, int e) { MyList<Integer> res = new MyDynamicList<Integer>

我一直在研究这种递归方法,但似乎无法理解它。我的方法是:我决定创建列表的副本,然后为递归调用删除副本的最后一个元素。如果最后一项(复制列表的最后一项,这是我尝试循环遍历列表的方式)大于给定的int,我也会将其从实际列表中删除

public MyList<Integer> reduceMyList(MyList<Integer> m, int e) {

MyList<Integer> res = new MyDynamicList<Integer>();

for (int i = 0; i < m.length(); i++) {

    res.addElement(i, m.getElement(i));
}

int scenario = 0;

if (m.length() <= 1) {

    scenario = 1;
} else {

    scenario = 2;
}

switch (scenario) {

case 1:
    if(res.length() == 0){
        System.out.println();
        break;
    }       

case 2:



    if (e <= res.getElement(res.length() - 1)) {

        m.removeElement(res.length() - 1);


    }
    reduceMyList(m, e);

    res.removeElement(res.length() - 1);

    break;
}
return m;
}
public MyList reduceMyList(MyList m,int e){
MyList res=新的MyDynamicList();
对于(int i=0;ipublic MyList reduceMyList(MyList m,int e){
如果(m.size()==0)
返回新的MyDynamicList();
MyList subList=m.subList(0,m.size()-1);//获取不包含最后一个元素的列表
整数lastInteger=m.get(m.size()-1);
MyList reducedList=reduceMyList(subList,e);//对subList的递归调用
if(lastInteger
public MyList reduceMyList(MyList m,int e){
如果(m.size()==0)
返回新的MyDynamicList();
MyList subList=m.subList(0,m.size()-1);//获取不包含最后一个元素的列表
整数lastInteger=m.get(m.size()-1);
MyList reducedList=reduceMyList(subList,e);//对subList的递归调用

如果(lastInteger我意识到我需要从列表的前面删除,并且我过于复杂了,那么我的错误。我使用一个新列表来存储小于给定int的元素的值,并返回该值

public MyList<Integer> reduceMyList(MyList<Integer> m, int e) {

    MyList<Integer> res = null;
    res = new MyDynamicList<Integer>();

    int scenario = 0;

    if (m.length() == 0) {
        scenario = 1;

    }

    else {
        scenario = 2;
    }

    switch (scenario) {

    case 1:

         System.out.println();
        break;
    case 2:

        int e0 = m.getElement(0);
        m.removeElement(0);

        res = reduceMyList(m, e);

        if (e0 < e) {

            res.addElement(0, e0);
        }
        m.addElement(0, e0);
    }
    return res;
}
public MyList reduceMyList(MyList m,int e){
MyList res=null;
res=新的MyDynamicList();
int场景=0;
如果(m.length()==0){
情景=1;
}
否则{
情景=2;
}
切换(场景){
案例1:
System.out.println();
打破
案例2:
int e0=m.getElement(0);
m、 移除元素(0);
res=还原列表(m,e);
if(e0
我意识到我需要从列表的前面删除,这太复杂了,我的错。我使用一个新的列表来存储小于给定int的元素的值,并返回该值

public MyList<Integer> reduceMyList(MyList<Integer> m, int e) {

    MyList<Integer> res = null;
    res = new MyDynamicList<Integer>();

    int scenario = 0;

    if (m.length() == 0) {
        scenario = 1;

    }

    else {
        scenario = 2;
    }

    switch (scenario) {

    case 1:

         System.out.println();
        break;
    case 2:

        int e0 = m.getElement(0);
        m.removeElement(0);

        res = reduceMyList(m, e);

        if (e0 < e) {

            res.addElement(0, e0);
        }
        m.addElement(0, e0);
    }
    return res;
}
public MyList reduceMyList(MyList m,int e){
MyList res=null;
res=新的MyDynamicList();
int场景=0;
如果(m.length()==0){
情景=1;
}
否则{
情景=2;
}
切换(场景){
案例1:
System.out.println();
打破
案例2:
int e0=m.getElement(0);
m、 移除元素(0);
res=还原列表(m,e);
if(e0
您的代码中有很多有趣的东西。首先,似乎您正在使用类型作为变量(MyList、MyDynamicList),但您没有给我们定义(它们看起来只是扩展了一些列表)。你可能不需要定义这些东西,尽管你可以。其次,没有理由为此使用递归。但是如果你真的想,你可以在过程中递归、测试和删除一个项目。这实际上只是将递归更改为迭代,而且有点毫无意义(更不用说效率低下):

import java.util.*;
公共类MyList扩展了ArrayList{
公共空间减少(INTE)
{
减少(尺寸()-1,e);
}
私有空间缩减(内部位置,内部e)
{
如果(位置<0){
返回;
}
if(get(pos)
使用递归的另一种方法是在每个递归级别复制列表,并将其传递到下一个级别,然后返回最终列表。这将更加低效

要以迭代方式执行此操作,可以使用如下方法:

// iterating backward as that's more efficient when removing items
public void reduceIter(int e) {
    for (int i=size()-1; i >= 0; i--) {
        if (get(i) < e) {
            remove(i);
        }
    }
}
//向后迭代,因为这样在删除项时效率更高
公共空隙减少器(int e){
对于(int i=size()-1;i>=0;i--){
如果(得到(i)
根据您的评论和对该问题的回答,如果您希望通过删除原始列表中的项目然后重新添加项目来递归,那么您的函数可以如下所示(请注意,您的解决方案不会从原始列表中删除低于该值的项,正如本问题标题中所要求的那样——它会生成低于该值的项的新列表):

public MyList reduceMyList(MyList m,int min){
如果(m.length()==0){
//不知道为什么要打印,但在这里
System.out.println();
返回新的MyDynamicList();
}
int值=m.getElement(0);
m、 移除元素(0);
MyList res=还原列表(m,min);
如果(值<最小值)
{
res.addElement(0,值);
}
m、 加法(0,值);
返回res;
}

您的代码中有很多有趣的东西。首先,似乎您正在使用类型作为变量(MyList、MyDynamicList),但您没有给我们定义(它们看起来只是扩展了一些列表)。你可能不需要定义这些东西,尽管你可以。其次,没有理由为此使用递归。但是如果你真的想,你可以在过程中递归、测试和删除一个项目。这实际上只是将递归更改为迭代,而且有点毫无意义(更不用说效率低下):

import java.util.*;
公共类MyList扩展了ArrayList{
公共空间减少(INTE)
{
减少(尺寸()-1,e);
}
私有空间缩减(内部位置,内部e)
{
如果(位置<0){
返回;
}
if(get(pos)
另一种使用方法
// iterating backward as that's more efficient when removing items
public void reduceIter(int e) {
    for (int i=size()-1; i >= 0; i--) {
        if (get(i) < e) {
            remove(i);
        }
    }
}
public MyList<Integer> reduceMyList(MyList<Integer> m, int min) {

    if (m.length() == 0) {
        // not sure why you are printing, but here it is
        System.out.println();
        return new MyDynamicList<Integer>();
    }

    int value = m.getElement(0);
    m.removeElement(0);

    MyList<Integer> res = reduceMyList(m, min);
    if (value < min)
    {
        res.addElement(0, value);
    }

    m.addElement(0, value);
    return res;
}