- import java.util.*;
- class Demo2
- {
- private static int NUM = 4;
- private static List<Integer>al1 = new ArrayList<Integer>();
- public static void main(String[] args)
- {
- Integer []integer = {1,9,4,9};
- List<Integer>list = new ArrayList<Integer>(Arrays.asList(integer));
- method(list,new ArrayList<Integer>());
- for(Integer i : al1)
- {
- if(suShu(i))
- System.out.println(i);
- }
- }
- private static void method(List<Integer>list,List<Integer>list1){
-
-
- if(list1.size()== NUM){
- Integer [] arr = list1.toArray(new Integer[list1.size()]);
- StringBuilder sb = new StringBuilder();
- for(int x=0;x<arr.length;x++){
- sb.append(arr[x]);
- }
- String s = new String(sb);
- al1.add(Integer.parseInt(s));
-
- return;
- }
-
- //System.out.println(list.size());
- for(int x=0;x<list.size();x++){
-
- List<Integer>newList = new ArrayList<Integer>(list);
- List<Integer>newList1 = new ArrayList<Integer>(list1);
- newList1.add(newList.get(x));
- newList.remove(x);
- method(newList,newList1);
-
- }
- }
- public static boolean suShu(int i)//定义一个判断是否为素数的方法。
- {
- int count = 0;
- if (i == 1)
- return false;
- for(int x=1;x<=i;x++)
- {
- if((i%x)==0)
- count++;
- }
- if(count>2)
- return false;
- else
- return true;
-
- }
- }
复制代码
|