Suppose I have the following String: "10 30 20 12 34". Now I want to rotate the String but keep the first and last integers fixed. So I should get the 3 outputs should be as follows:
10 20 30 12 34
10 30 12 20 34
10 12 20 30 34
Now, I am trying to first convert the String into an int[] array such that it looks like [10, 30,20, 12, 34] and then create another array, get each element of the original array and insert them in the new array.
This is what I have so far:
String[] arr1 = tree.getPreOrder().split(" ");
int[] arr2 = new int[5];
arr2[0] = Integer.parseInt(arr1[0]);
arr2[1] = Integer.parseInt(arr1[3]);
arr2[2] = Integer.parseInt(arr1[2]);
arr2[3] = Integer.parseInt(arr1[1]);
arr2[4] = Integer.parseInt(arr1[4]);
My issue is, how do I now convert arr2 into the format 10 12 20 30 34. I tried to use join() but it doesn't work.
Also, is there a simpler way to do what I'm trying to do as currently, I am hard coding those values.
Thanks
UPDATE I created a method which basically rotates the middle values now. However, I got into an issue where one randomly generated String was the same as the original and I would like to avoid that. I'm basically trying to generate only exactly 3 variations of the original string if that helps. Here's my updated code:
static String randomizeAnswer(String ans) {
    String[] arr = ans.split(" ");
    int arraySize = arr.length;
    String firstElement = arr[0];
    String lastElement = arr[arraySize - 1];
    String[] arr2 = new String[arraySize - 2];
    String arr3[] = new String[arraySize];
    arr3[0] = firstElement;
    for (int i = 0; i < arraySize - 2; i++) {
        arr2[i] = arr[i + 1];
    }
    Collections.shuffle(Arrays.asList(arr2));
    for (int j = 0; j < arr2.length; j++) {
        arr3[j + 1] = arr2[j];
    }
    arr3[arraySize - 1] = lastElement;
    return String.join(" ", arr3);
}
for int(i = 0; i<3; i++){
    System.out.println(randomizeAnswer("10 30 20 12 34"));
}
UPDATE 2 I eventually managed to make a solution which works but the answer by @WJS below is much better.
static String randomizeAnswer(String ans) {
        String[] arr = ans.split(" ");
        int arraySize = arr.length;
        String firstElement = arr[0];
        String lastElement = arr[arraySize - 1];
        String[] arr2 = new String[arraySize - 2];
        String arr3[] = new String[arraySize];
        arr3[0] = firstElement;
        for (int i = 0; i < arraySize - 2; i++) {
            arr2[i] = arr[i + 1];
        }
        Collections.shuffle(Arrays.asList(arr2));
        for (int j = 0; j < arr2.length; j++) {
            arr3[j + 1] = arr2[j];
        }
        arr3[arraySize - 1] = lastElement;
        return String.join(" ", arr3);
        
    }
    static String[] generateAnswers(String ans) {
        String[] answers = new String[5];
        answers[0] = ans;
        answers[4] = "None of the answers are correct";
        for (int x = 0; x < 3; x++) {
            while (true) {
                String randomAns = randomizeAnswer(ans);
                if (Arrays.stream(answers).anyMatch(randomAns::equals)) {
                    continue;
                } else {
                    answers[x+1] = randomAns;
                    break;
                }
            }
        }
        return answers;
    }
 
     
     
     
     
    