通过递归获得arraylist的奇数/偶数元素

时间:2014-04-10 03:36:53

标签: java recursion arraylist

我制作了一个程序,它采用arraylist中的元素列表并使用递归,获取偶数和奇数元素。例如,如果它是{1,2,3,4,5,6}。它将返回{1,3,5},因为它们具有偶数元素放置。

我想出了如何顺利地为偶数做到这一点,但我似乎无法让它适用于赔率。

以下是错误

java.lang.ArrayIndexOutofBoundsException:
-1 (in java.util.ArrayList)

这是我的偶数代码

public static ArrayList<Integer> even(ArrayList<Integer> tList)
{
    ArrayList<Integer> newList = ListMethods.deepClone(tList); 
    int temp = newList.size();
    if (newList.size()<=0)// The list is empty or has one element)
    {
        return newList;// Return the list as is – no need to reverse!
    }
    else
    {
        if(newList.size()%2==0)
            temp = newList.remove(newList.size()-2);
        newList.remove(newList.size()-1);
        newList = ListMethods.even(newList);
        if (temp!=0)
            newList.add(temp);
    }
    return newList;
}

奇数代码:(这是我收到错误的地方)

public static ArrayList<Integer> odd(ArrayList<Integer> tList)
{
    ArrayList<Integer> newList = ListMethods.deepClone(tList); 
    int temp = newList.size();
    if (newList.size()<=0)// The list is empty or has one element)
    {
        return newList;// Return the list as is – no need to reverse!
    }
    else
    {
        if(newList.size()%2==1)
            temp = newList.remove(newList.size()-1);
        newList.remove(newList.size()-1);
        newList = ListMethods.odd(newList);
        if (temp!=0)
            newList.add(temp);
    }
    return newList;
}

深度克隆:

public static ArrayList<Integer> deepClone(ArrayList<Integer> tList)
{
    ArrayList<Integer> list = new ArrayList<Integer>(); 
    for (Integer i : tList)
    {
        list.add(new Integer(i));
    }
    return list;
}

我的测试人员代码:

import java.util.ArrayList;
import java.util.Scanner;
public class ListMethodsRunner
{
public static void main(String[] args) 
{
    ArrayList<Integer> tempList = ListMethods.makeList(100);
    System.out.println("What would you like to do to this ArrayList?  Type the number.");
    System.out.println("1. Show Even Elements");
    System.out.println("2. Show Odd Elements");
    System.out.println(" ");
    Scanner input = new Scanner(System.in);
    int z = input.nextInt();
    if(z==1)
        tempList = ListMethods.even(tempList);
    if(z==2)
        tempList = ListMethods.odd(tempList);

    if (tempList.size() == 0)
    {
        System.out.println("The list is empty");
    }
    else
    {
        for (Integer i : tempList)
        {
            System.out.println(i);
        }
    }

}

}

2 个答案:

答案 0 :(得分:0)

没关系,我自己想出来了。

public static ArrayList<Integer> odd(ArrayList<Integer> tList)
{
   ArrayList<Integer> newList = ListMethods.deepClone(tList); 
   int temp = newList.size();
   if (newList.size()<=0)// The list is empty or has one element)
   {
       return newList;// Return the list as is – no need to reverse!
   }
   else
   {
       if(newList.size()%2==0) // I had `1` here instead of `0`
          temp = newList.remove(newList.size()-1);
       newList.remove(newList.size()-1);
       newList = ListMethods.odd(newList);
       if (temp!=0)
        newList.add(temp);
}
return newList;

答案 1 :(得分:0)

我不确定上面的代码是否有用,以防您有奇数条目可能您可以使用以下内容:

public static ArrayList<Integer> returnList(ArrayList<Integer> tList,boolean flag){
        int size=tList.size();
        int t;
        //print odd positions - flag is true
        if(flag){
           if(size>0 && size%2==0){
                t = tList.remove(size-1);               
                tList=returnList(tList,flag);
                tList.add(t);
           }
            else if(size%2 == 1){
                t = tList.remove(size-1);
                tList=returnList(tList,flag);
            }
            else{
            }

            System.out.println("Printing.."+tList);
        }
        else{

        }
        return tList;

    }