1个回答
2013-11-02
展开全部
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Test t = new Test();
t.contList();
t.getAllArray(list, 0);
}
private static List<Integer> list = new ArrayList();
private void contList(){
list.add(1);
list.add(2);
list.add(3);
}
public void getAllArray(List<Integer> inlist,int site){
int tempsite = site;
if(site >= inlist.size()){
return;
}
Integer firstNode = inlist.get(site++);
List tempList = new ArrayList(inlist);
tempList.remove(tempsite);
for(int i = 0;i < tempList.size();i++){
System.out.print(firstNode);
for(int j = i;j < tempList.size()+i;j++){
if(j < tempList.size()){
System.out.print(","+tempList.get(j));
}else{
System.out.print(","+tempList.get(j-tempList.size()));
}
}
System.out.println("");
}
getAllArray(inlist,site);
}
}
n=3这个运行结果是
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
String[] array = new String[] { "1", "2", "3", "4" };
int length=array.length;
listAll(Arrays.asList(array), "",length);
}
public static void listAll(List candidate, String prefix,int length) {
if(prefix.length()==length)
System.out.println(prefix);
for (int i = 0; i < candidate.size(); i++) {
List temp = new LinkedList(candidate);
listAll(temp, prefix + temp.remove(i),length);
}
}
}
import java.util.List;
public class Test {
public static void main(String[] args) {
Test t = new Test();
t.contList();
t.getAllArray(list, 0);
}
private static List<Integer> list = new ArrayList();
private void contList(){
list.add(1);
list.add(2);
list.add(3);
}
public void getAllArray(List<Integer> inlist,int site){
int tempsite = site;
if(site >= inlist.size()){
return;
}
Integer firstNode = inlist.get(site++);
List tempList = new ArrayList(inlist);
tempList.remove(tempsite);
for(int i = 0;i < tempList.size();i++){
System.out.print(firstNode);
for(int j = i;j < tempList.size()+i;j++){
if(j < tempList.size()){
System.out.print(","+tempList.get(j));
}else{
System.out.print(","+tempList.get(j-tempList.size()));
}
}
System.out.println("");
}
getAllArray(inlist,site);
}
}
n=3这个运行结果是
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
String[] array = new String[] { "1", "2", "3", "4" };
int length=array.length;
listAll(Arrays.asList(array), "",length);
}
public static void listAll(List candidate, String prefix,int length) {
if(prefix.length()==length)
System.out.println(prefix);
for (int i = 0; i < candidate.size(); i++) {
List temp = new LinkedList(candidate);
listAll(temp, prefix + temp.remove(i),length);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询