I'm trying to write a method that receives a List of strings, and returns subsets that contains n elements. Here is my code:
import Pack;
import java.util.List;
import java.util.ArrayList;
public class Testtt{
public static void main(String[] args) {
List<String> input = new ArrayList<String>();
input.add("a");
input.add("b");
input.add("c");
input.add("d");
input.add("e");
System.out.println(subset(input, 3));
}
public static List<List<String>> subset( List<String> inputlist, int n) {
List<List<String>> output = new ArrayList<List<String>>();
List<String> temp = new ArrayList<String>();
for (int i = 0; i < n; i++) {
temp = inputlist;
System.out.println("Temp = " +temp);
temp.remove(i);
output.add(temp);
}
return output;
}
}
Output was containing only 2 elements i decided to debug and this is the console output:
Temp = [a, b, c, d, e]
Temp = [b, c, d, e]
Temp = [b, d, e]
[[b, d], [b, d], [b, d]]
I've written temp = list at the beginning of the loop but it stays the same. Why this is happening?