public static void main(String[] args) {
List srcList = getTestData();
show(srcList, "--data before allocation--");
List dstList = solve(srcList);
show(dstList, "--data after allocation--");
}
public static void show(List list, String s) {
System.out.println(s);
for (int i = 0; i < list.size(); i++) {
Collection c = (Collection) list.get(i);
show(c.toArray());
}
}
public static void show(Object[] o) {
System.out.print("{");
for (int i = 0; i < o.length; i++) {
System.out.print(o[i]);
if (i + 1 != o.length)
System.out.print(",");
}
System.out.println("}");
}
public static List solve(List srcList) {
if (srcList == null)
throw new NullPointerException();
List tempList = new ArrayList();
tempList.add(new HashSet());
while (srcList.size() > 0) {
List tList = (List) srcList.get(0);
tempList = Allocate(tempList, tList);
srcList.remove(0);
}
return tempList;
}
public static List getTestData() {
String[][] s = new String[][] { new String[] { "a", "b" },
new String[] { "c", }, new String[] { "a", "e" } };
List list = new ArrayList();
for (int i = 0; i < s.length; i++) {
List subList = new ArrayList();
for (int j = 0; j < s[i].length; j++) {
subList.add(s[i][j]);
}
list.add(subList);
}
return list;
}
public static List Allocate(List dst, List src) {
List tempList = new ArrayList();
int index = 0;
do {
for (int j = 0; j < src.size(); j++) {
Set s = new HashSet((Set) dst.get(index));
s.add(src.get(j));
tempList.add(s);
}
index++;
} while (index < dst.size());
return tempList;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> a = new ArrayList<String>();
a.add("ab");
a.add("c");
a.add("ae");
ArrayList<String> ts = new ArrayList<String>();
assemble(a.listIterator(), ts);