Java implements the full array array method

  • 2020-04-01 04:32:48
  • OfStack

This article illustrates a Java method for implementing full array arrays. Share with you 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, simply press the array to print
   for (int i = 0; i <= end; i++) {
    System.out.print(buf[i]);
   }
   System.out.println();
  } else {//Multiple letters all arranged
   for (int i = start; i <= end; i++) {
    char temp = buf[start];//Swap the first and subsequent elements of the array
    buf[start] = buf[i];
    buf[i] = temp;
    permutation(buf, start + 1, end);//Subsequent elements are recursively arranged
    temp = buf[start];//Restores 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 results:

ABC
acb
bac
bca
The cba
cab

I hope this article has been helpful to you in Java programming.


Related articles: