How to shift the contents of an array?

user13060550 :

I need a method that produces the following output for the given input:

Input:

4, 5, 1, 6, 2 | 6, 1, 0 | 9, 3, 1, 5, 2, 0, 4, 6, 7, 8

Output:

5, 1, 6, 2, 4 | 1, 0, 6 | 3, 1, 5, 2, 0, 4, 6, 7, 8, 9

The program prompts the user to enter multiple arrays of ints, separated by '|'. The program converts the user input from a String to multiple arrays of ints. It has a method that has an array of ints as a parameter. The method moves every element in the array one position to the left; i.e. the value in element 1 is moved to element 0, the value in element 2 is stored in element 1, and so on. Finally, the value in the first element is moved around to the last position.

You must use a for loop to rotate the array. Note that you will need to store the value of element 0 in a temporary variable so that you can later move it into the last element once all the other elements have been moved.

//Below is my code so far:

public class IntArrayRotation {

    public static void main(String[] args) {
        IntArrayRotation prog = new IntArrayRotation();
        prog.start();
    }

    //Begin your code here
    public void start() {
        int[] data = {4, 5, 1, 6, 2};
        int[] result = new int[data.length];
        for (int i = 0; i < data.length; i++) {
            result[(i + (data.length - 1)) % data.length] = data[i];
        }

        for (int i : result) {
               System.out.println(i);
        }
    }
}
Arvind Kumar Avinash :

Do it as follows:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter multiple arrays of ints, separated by '|': ");
        String input = in.nextLine();

        // Split the input on '|'
        String[] substrs = input.split("\\|");

        StringBuilder sb = new StringBuilder();

        for (String substr : substrs) {// Iterate substrs []

            // Split each substr on ','
            String arr[] = substr.split(",");

            // Rotate arr[] as per the requirement
            rotate(arr);

            // String representation of array e.g. [1, 2, 3]
            String str = Arrays.toString(arr);

            // Remove '[' from the string representation of array
            str = str.substring(1);

            // Remove ']' from the string representation of array
            str = str.substring(0, str.length() - 1);

            // Append the remaining part of the string representation of the array along
            // with a '|' at the end
            sb.append(str).append("|");
        }
        sb.deleteCharAt(sb.length() - 1);// Delete the last '|'
        System.out.println("After rotation: " + sb);
    }

    static void rotate(String[] arr) {
        // Store the first element
        String temp = arr[0];

        for (int i = 1; i < arr.length; i++) {
            // Shift all elements starting from index 1 to the left by one place
            arr[i - 1] = arr[i];
        }

        // put the first element at the end
        arr[arr.length - 1] = temp;
    }
}

A sample run:

Enter multiple arrays of ints, separated by '|': 4, 5, 1, 6, 2|6, 1, 0|9, 3, 1, 5, 2, 0, 4, 6, 7, 8
After rotation:  5,  1,  6,  2, 4| 1,  0, 6| 3,  1,  5,  2,  0,  4,  6,  7,  8, 9

I have put the required comments in the code so that it will be easier for you to understand. Feel free to comment in case of any doubt/issue.

[Update]

OP: As you have requested, I've posted below the same program without using a StringBuilder. Feel free to comment in case of any doubt/issue.

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter multiple arrays of ints, separated by '|': ");
        String input = in.nextLine();

        // Split the input on '|'
        String[] substrs = input.split("\\|");

        String result = "";

        for (String substr : substrs) {// Iterate substrs []

            // Split each substr on ','
            String arr[] = substr.split(",");

            // Rotate arr[] as per the requirement
            rotate(arr);

            // String representation of array e.g. [1, 2, 3]
            String str = Arrays.toString(arr);

            // Remove '[' from the string representation of array
            str = str.substring(1);

            // Remove ']' from the string representation of array
            str = str.substring(0, str.length() - 1);

            // Append the remaining part of the string representation of the array along
            // with a '|' at the end
            result += str + "|";
        }
        result = result.substring(0, result.length() - 1);// Drop the last '|'
        System.out.println("After rotation: " + result);
    }

    static void rotate(String[] arr) {
        // Store the first element
        String temp = arr[0];

        for (int i = 1; i < arr.length; i++) {
            // Shift all elements starting from index 1 to the left by one place
            arr[i - 1] = arr[i];
        }

        // put the first element at the end
        arr[arr.length - 1] = temp;
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=393466&siteId=1