This article describes the method of implementing full arrangement of character arrays in Java. Share it for your reference, as follows:
import org.junit.Test;public class AllSort { public void permutation(char[] buf, int start, int end) { if (start == end) {// When only one letter in the array is required to be fully arranged, just output it according to the array for (int i = 0; i <= end; i++) { System.out.print(buf[i]); } System.out.println(); } else {// Fully arranged for (int i = start; i <= end; i++) { char temp = buf[start];// swap the first element of the array and the subsequent elements buf[start] = buf[i]; buf[i] = temp; permutation(buf, start + 1, end);// The subsequent elements are fully arranged recursively temp = buf[start];// Restore the swapped array buf[start] = buf[i]; buf[i] = temp; } } } @Test public void testPermutation() throws Exception { char[] buf = new char[] { 'a', 'b', 'c' }; permutation(buf, 0, 2); } }Run the test and output the result:
abc
acb
bac
bca
cba
cab
I hope this article will be helpful to everyone's Java programming.