Kirat
Kirat

Reputation: 89

Permutation code without repeating

I have a code with permutation and when I run this code, it returns multiple results with the same value.
I want to not return the same result, to break the repetition.

This is my code:

public class Teste {

    private static final char[] MP = new char[] {'i', 't', '@', 'r', 'm', 't', 'z', 'c', 'r', '.', 'c', 'a', 'a', 't', 't', 'c', 'b', 'a', 'i', '.', 'o'};

    private static final int[][] SWAPS = new int[][] {
        {2, 8, 18, 4, 0, 15, 1, 11, 3, 14, 20, 5, 19, 16, 13, 6, 12, 10, 17, 9, 7},
        {15, 8, 6, 2, 17, 14, 4, 9, 0, 5, 13, 7, 3, 19, 10, 18, 20, 11, 16, 12, 1},
        {0, 17, 20, 7, 6, 3, 14, 2, 18, 15, 19, 8, 16, 1, 5, 9, 13, 12, 11, 4, 10},
        {12, 11, 15, 14, 10, 4, 0, 19, 13, 17, 3, 9, 20, 6, 8, 16, 1, 7, 5, 18, 2}
    };

    public static void main(String[] args) {
        char[] str =  MP;
        int[][] out ;
        Teste p = new Teste() ;

        while (p.SWAPS != null) {
            out = p.SWAPS;

            for ( char e : MP ) {
                System.out.print(e + " - ") ;
            }
            System.out.println() ;
        }
    }
}

Upvotes: 0

Views: 95

Answers (1)

David Choweller
David Choweller

Reputation: 1050

Your code does not seem to use the SWAPS array at all.

Do you mean to use the SWAPS array in some fashion to decide which index from MP you want to print?

Also, you have a while loop that checks the value of p.SWAPS. However this value never changes within the loop, so you'll get an infinite loop.

If you want to choose a random character from the MP array you can use the java.util.Random class.

I saw your clarification above where you specify how to find the position (8) for the first character in the MP array. How do you find the position for the rest of the characters in the MP array. If you start of at 0,0 every time, all of the characters will end up at the 8th index.

Upvotes: 1

Related Questions