简体   繁体   English

通过递归获取数组列表的奇/偶元素

[英]Getting odd/even elements of an arraylist by recursion

I'm making a program where it takes a list of elements in an arraylist and using recursion, gets the even and odd elements. 我正在编写一个程序,其中它使用arraylist中的元素列表并使用递归,获得偶数和奇数元素。 For instance, if it were {1,2,3,4,5,6}. 例如,如果它是{1,2,3,4,5,6}。 It would return {1,3,5} because they have an even element placement. 它会返回{1,3,5},因为它们的元素位置是偶数。

I figured out how to do it for even numbers without a hitch, but I can't seem to make it work for odds. 我想出了如何对偶数无障碍地做到这一点,但是我似乎无法使它发挥作用。

Here is the error : 这是错误

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

Here is my even code : 这是我的偶数代码

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;
}

Odd Code: (this is where I get the error) 奇数代码:(这是我收到错误的地方)

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;
}

Deep Clone: 深克隆:

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;
}

My Tester Code: 我的测试人员代码:

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);
        }
    }

}

} }

Nevermind guys, I figured it out by myself. 没关系,我自己弄清楚了。

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;

I am not sure if the above code will work in case you have odd number of entries May be you can use something like below : 我不确定上面的代码是否可以在条目数为奇数的情况下使用,可能是您可以使用以下代码:

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;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM