Answer:
Here's an example Java code that can print all permutations of names given as input:
Step-by-step explanation:
import java.util.Scanner;
public class Permutations {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] names = new String[10];
int n = 0;
// read the names from input and store them in an array
while (input.hasNext()) {
String name = input.next();
if (name.equals("-1")) {
break;
}
names[n++] = name;
}
// call the method to print all permutations of names
printPermutations(names, 0, n-1);
}
// recursive method to print all permutations of names
public static void printPermutations(String[] names, int left, int right) {
if (left == right) {
// base case: if left and right are equal, it means we have reached the end of the name list
for (int i = 0; i <= right; i++) {
if (i == right) {
System.out.print(names[i]); // print the last name without a comma
} else {
System.out.print(names[i] + ", "); // print the name with a comma after it
}
}
System.out.println(); // print a new line after printing the names
} else {
// recursive case: perform permutations of names at different positions
for (int i = left; i <= right; i++) {
// swap the name at position i with the name at position left
String temp = names[left];
names[left] = names[i];
names[i] = temp;
// perform permutations at position left+1
printPermutations(names, left+1, right);
// swap the name at position i back to its original position
names[i] = names[left];
names[left] = temp;
}
}
}
}