您需要N个嵌套循环,这使它变得很难。
您可以使用递归来实现。
static <E> ArrayList combine(ArrayList<E> soFar, ArrayList<E>... lists){ // Rather than constantly making and remaking this list could just use one // and pass it around and add stuff to it. This works though. ArrayList<ArrayList<E>> combs=new ArrayList<ArrayList<E>>(); // Loop through the first list looking for elements for(E e:lists[0]) { // Create a new List to build this combination ArrayList<E> temp = new ArrayList<>(soFar); // Add this element to the combination temp.add(e); // If there are more lists recurse down if (lists.length > 1) {// Use recursion to add all combinations of the remaining listscombs.addAll(combine(temp, lists.subList(1))); } else {// There are no more lists so we are done, add temp to the comboscombs.add(temp); } } return combs;}// Call this method to start things going, the other one should probably be privatestatic <E> ArrayList combine(ArrayList<E>... lists) return combine(new ArrayList<E>(), lists);}


