因为 if(b<e)这块,我用了个while,造成 了死循环。郁闷。
查了半天,才发现,写错了。
下面是正确的代码
package com.project;
/**
* 通过递归实现数组中前后位置颠倒
*
* @author Seliat
*
*/
public class IterativeReverseArray {
public static int[] intArray;
public IterativeReverseArray()
{
}
public int[] execute(int[] intArrays,int b,int e)
{
intArray=intArrays;
if(b<e)
{
//交换
swap(b,e);
//递归调用,直到交换完毕
intArray=execute(intArray,b+1,e-1);
}
return intArray;
}
public static void swap(int beforeint, int affterint)
{
int temp = intArray[affterint];
intArray[affterint] = intArray[beforeint];
intArray[beforeint]= temp;
}
public static void main(String args[])
{
//递归方式,对数组位置前后调换
IterativeReverseArray itera=new IterativeReverseArray();
int[] argsInts={1,2,3,4,5,6,7};
int[] returnInt=itera.execute(argsInts,0,6);
for(int i=0;i<returnInt.length;i++)
{
System.out.println("swap="+returnInt[i]);
}
}
}