Question: Can you explain the allPermutations function for me? How does one even come up with a solution like this? import java.util.Scanner; import java.util.ArrayList; public class

Can you explain the allPermutations function for me?

How does one even come up with a solution like this?

import java.util.Scanner; import java.util.ArrayList;

public class PhotoLineups {

// TODO: Write method to create and output all permutations of the list of names. public static void allPermutations(ArrayList permList, ArrayList nameList) { if (nameList.isEmpty()) { for (int i = 0; i < permList.size(); i++) { System.out.print(permList.get(i) + " "); } System.out.println(); } else { for (int i = 0; i < nameList.size(); ++i) { ArrayList newPerm = new ArrayList(permList); newPerm.add(nameList.get(i)); ArrayList newNameList = new ArrayList(nameList); newNameList.remove(i); allPermutations(newPerm, newNameList); } } }

public static void main(String[] args) { Scanner scnr = new Scanner(System.in); ArrayList nameList = new ArrayList(); ArrayList permList = new ArrayList(); String name; // TODO: Read in a list of names; stop when -1 is read. Then call recursive method. while (true) { name = scnr.next(); if (name.equals("-1")) break; nameList.add(name); } allPermutations(permList, nameList); scnr.close(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!