package ppt;
import java.util.ArrayList;
public class Demo22_1
{
private static ArrayList<Integer> tmp = new ArrayList<Integer>();
public static void main(String[] args) {
int[] arr = new int[] {1,2,3,4,5,6,7,8};
repeatArr(100,arr);
}
public static void repeatArr(int k,int[] arr)
{
if(k==1)
{
for(int i=0;i<arr.length;i++)
{
tmp.add(arr[i]);
System.out.println(tmp.toString());
tmp.remove(tmp.size()-1);
}
}
else if(k>1)
{
for(int i=0;i<arr.length;i++)
{
tmp.add(arr[i]);
repeatArr(k-1,arr);
tmp.remove(tmp.size()-1);
}
}
else
{
return;
}
}
import java.util.ArrayList;
public class Demo22_1
{
private static ArrayList<Integer> tmp = new ArrayList<Integer>();
public static void main(String[] args) {
int[] arr = new int[] {1,2,3,4,5,6,7,8};
repeatArr(100,arr);
}
public static void repeatArr(int k,int[] arr)
{
if(k==1)
{
for(int i=0;i<arr.length;i++)
{
tmp.add(arr[i]);
System.out.println(tmp.toString());
tmp.remove(tmp.size()-1);
}
}
else if(k>1)
{
for(int i=0;i<arr.length;i++)
{
tmp.add(arr[i]);
repeatArr(k-1,arr);
tmp.remove(tmp.size()-1);
}
}
else
{
return;
}
}
}
递归式很好,可是当我运算10层k的时候递归就太慢了,一层套一层