Java 通过递归获取arraylist的奇偶元素
我正在制作一个程序,它在arraylist中获取元素列表,并使用递归,获取偶数和奇数元素。例如,如果它是{1,2,3,4,5,6}。它将返回{1,3,5},因为它们的元素位置是偶数 我想出了如何不费吹灰之力地处理偶数,但我似乎无法处理赔率问题 以下是错误:Java 通过递归获取arraylist的奇偶元素,java,recursion,arraylist,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(ArrayL
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;
}
这是我的偶数代码:
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;
}
公共静态ArrayList偶数(ArrayList tList)
{
ArrayList newList=ListMethods.deepClone(tList);
int temp=newList.size();
如果(newList.size()Nevermind伙计们,我自己想出来的
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;
公共静态ArrayList奇数(ArrayList tList)
{
ArrayList newList=ListMethods.deepClone(tList);
int temp=newList.size();
如果(newList.size()如果您有奇数个条目,我不确定上面的代码是否有效,您可以使用如下代码:
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;
}
公共静态ArrayList返回列表(ArrayList TLList,布尔标志){
int size=tList.size();
int t;
//打印奇数位置-标志为真
国际单项体育联合会(旗){
如果(大小>0&&size%2==0){
t=t列表。移除(尺寸-1);
tList=返回列表(tList,标志);
t增加(t);
}
else if(大小%2==1){
t=t列表。移除(尺寸-1);
tList=返回列表(tList,标志);
}
否则{
}
System.out.println(“打印..”+tList);
}
否则{
}
返回列表;
}
Yikes!odd()
和偶数()
实际上是相同的。将它们的代码合并到一个函数中,并将布尔值
传递给该函数,以确定它是充当odd()
还是偶数()
。为什么这里需要递归?这个列表是一个简单的整数列表。您是要获取奇偶元素还是要获取奇偶索引元素?@music\u coder我必须有两个独立的方法。@DnR假设arraylist是1,3,4,4,5,6,25。我想要赔率:3,4,6。偶数:1,4,5,25。
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;
}