Printing all possible subsets of a list

后端 未结 7 1368
半阙折子戏
半阙折子戏 2020-12-28 11:18

I have a List of elements (1, 2, 3), and I need to get the superset (powerset) of that list (without repeating elements). So basically I need to create a List of Lists that

7条回答
  •  臣服心动
    2020-12-28 11:50

    This is the simple function can be used to create a list of all the possible numbers generated by digits of all possible subsets of the given array or list.

    void SubsetNumbers(int[] arr){
        int len=arr.length;
        List list=new ArrayList();
        List list1=new ArrayList();
        for(int n:arr){
            if(list.size()!=0){
                for(int a:list){
                    list1.add(a*10+n);
                }
                list1.add(n);
                list.addAll(list1);
                list1.clear();
            }else{
                list.add(n);
            }
        }
        System.out.println(list.toString());
    }
    

提交回复
热议问题