Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 9 For Linux UNIX And Windows Advanced Database Administration Certification Certification Study Guide

Authors: Roger E. Sanders, Dwaine R Snow

1st Edition

1583470808, 978-1583470800

More Books

Students also viewed these Databases questions

Question

What lessons can businesses learn from this experience? p-687

Answered: 1 week ago