配列の内容をシフトするには?

user13060550:

私は与えられた入力に対して次の出力を生成する方法が必要になります。

入力:

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

出力:

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

「|」プログラムで区切られたint型の複数のアレイを入力を促します。プログラムは、int型の複数のアレイへの文字列からユーザ入力を変換します。これは、パラメータとしてintの配列を有する方法を有します。この方法は、左アレイ1つの位置にすべての要素を移動させます。要素1の値が0の要素に移動させる、すなわち、要素2の値は要素1内に格納されている、というようにされています。最後に、最初の要素の値は、最後の位置に周りに移動します。

あなたは、アレイを回転させるようにforループを使用する必要があります。あなたが他のすべての要素が移動された後、あなたが後で最後の要素に移動できるように、一時的な変数の要素0の値を格納する必要があることに注意してください。

//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);
        }
    }
}
アービンド・クマールのAvinash:

次のように実行します。

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;
    }
}

サンプルを実行します。

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

あなたが理解することが容易になりますように、私はコード内の必要なコメントを入れています。何の疑いも/問題の場合にはコメントをお気軽に。

[更新]

OP:あなたが要求してきたように、私は使用しなくても同じプログラムの下に掲載しましたStringBuilder何の疑いも/問題の場合にはコメントをお気軽に。

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;
    }
}

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=393532&siteId=1