首页 > 解决方案 > 如何移动数组的内容?

问题描述

我需要一种为给定输入生成以下输出的方法:

输入:

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

程序提示用户输入多个整数数组,用“|”分隔。该程序将用户输入从字符串转换为多个整数数组。它有一个以整数数组作为参数的方法。该方法将数组中的每个元素向左移动一个位置;即元素 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);
        }
    }
}

标签: javaarrays

解决方案


执行以下操作:

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

推荐阅读