Find a subset in Java

A class that asks for a subset in Java

import java.util.ArrayList;

public class Subset {
	/**Given array*/
	String[] list;
	/**Number of elements in a given array*/
	int count;
	/**List to store subsets*/
	ArrayList<String> arrayList = new ArrayList<>();

	 * @param list
	public Subset(String[] list) {
		this.list = list;
		count = list.length;
		arrayList = new ArrayList<>();
		//Find a subset
		for(int i=0;i<count;i++) {
			execute(i, "");

	 *Find a subset using a recall call based on arguments
	 * @param num
	 * @param str
	public void execute(int num, String str) {
		if(num < count) {
			if(str == "") {
				str = list[num];
			for(int i=num+1;i<count;i++) {
				execute(i, str + ", " + list[i]);

Execution example

public class Main {
	public static void main(String args[]) {
		String[] list = {"A", "B", "C", "D"};

		Subset subset = new Subset(list);

		for(String str:subset.arrayList) {


Execution result

A, B
A, B, C
A, B, C, D
A, B, D
A, C
A, C, D
A, D
B, C
B, C, D
B, D
C, D

Recommended Posts

Find a subset in Java
Cannot find javax.annotation.Generated in Java 11
3 Implement a simple interpreter in Java
I created a PDF in Java.
A simple sample callback in Java
Get stuck in a Java primer
About returning a reference in a Java Getter
What is a class in Java language (3 /?)
When seeking multiple in a Java array
Partization in Java
[Creating] A memorandum about coding in Java
Changes in Java 11
Java creates a table in a Word document
Rock-paper-scissors in Java
Java creates a pie chart in Excel
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Create a TODO app in Java 7 Create Header
Try making a calculator app in Java
Implement something like a stack in Java
Split a string with ". (Dot)" in Java
Creating a matrix class in Java Part 1
What Java programmers find useful in Kotlin
Pi in Java
FizzBuzz in Java
I made a primality test program in Java
GetInstance () from a @Singleton class in Groovy from Java
Read a string in a PDF file with Java
Create a CSR with extended information in Java
Be careful if you find SHIFT-JIS in Java
A story about the JDK in the Java 11 era
How to display a web page in Java
[Android / Java] Operate a local database in Room
Measure the size of a folder in Java
Try to create a bulletin board in Java
A note when you want Tuple in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I wrote a primality test program in Java
Let's create a super-simple web framework in Java
A bat file that uses Java in windows
Java tips-Create a Spring Boot project in Gradle
A quick review of Java learned in class
I wrote a prime factorization program in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
java build a triangle
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java