Java 集合。旋转不工作

Java 集合。旋转不工作,java,Java,我找不到集合。轮换工作。当我在userInputCharacters中输入qwerty并运行此代码时,它只会再次输出qwerty。有什么建议吗 Collections.rotate(Arrays.asList(userInputChararacter), 2); for(int index = 0; index <= characterAmount - 1; index++) System.out.print(userInputChararacter[index]); Colle

我找不到集合。轮换工作。当我在userInputCharacters中输入qwerty并运行此代码时,它只会再次输出qwerty。有什么建议吗

Collections.rotate(Arrays.asList(userInputChararacter), 2);
for(int index = 0; index <= characterAmount - 1; index++)
    System.out.print(userInputChararacter[index]);
Collections.rotate(Arrays.asList(userInputCharacter),2);

对于(int index=0;index它不起作用的原因是
Arrays.asList(新字符[]{'q','u','e'})
将创建大小为1而不是3的列表。它不同于
Arrays.asList('q','u','e'))
其中元素将正确自动装箱为字符和大小为3的列表。因此,您的旋转不会改变任何内容

您应该通过添加元素或为基础数组创建包装器类来创建自己的
列表
实例


或者,可能更容易完全删除
char
,甚至将单个字符表示为字符串。

请将其充实成一个字符串。无论你相信与否,这都会对你有所帮助,我实际上已经使用了该链接,但它仍然不起作用。不要在发布的代码中使用JavaScript代码段标记。我从你最初的帖子a中删除了这些标记在编辑中添加注释很长—为什么要重新添加它们?这不是JavaScript代码。抱歉,我没有看到添加普通旧Java代码的方法。值得一提的是,
数组。asList(新字符[]{q',u',e'})
的大小为3,将
字符
替换为
字符
可能是最简单的修复方法。
import java.util.*;
public class WrapAround
{
    public static void main(String[] args) throws java.io.IOException
    {
        //Create a scanner to read the users input.
        Scanner userInput = new Scanner(System.in);

        //Get the amount of characters the user wants to input.
        System.out.println("How many character do you want to enter?");
        int characterAmount = userInput.nextInt();

        //Get and put the user's characters into an array.
        System.out.println("Please enter your " + characterAmount + " characters.");
        String userInputString;
        userInputString = userInput.next();
        char[] userInputChararacter = new char[characterAmount];
        for(int index = 0; index <= characterAmount - 1; index++)
            userInputChararacter[index] = userInputString.charAt(index);

        //Get what number character the user wants to start with.
        System.out.println("Which character do you want to start with?");
        int startingCharacterIndex;
        startingCharacterIndex = userInput.nextInt();
        startingCharacterIndex--;

        //Give the user their characters in order.
        System.out.println("Here are all your characters, beginning with number " + (startingCharacterIndex + 1) + ".");
        userInputChararacter = Collections.rotate(Arrays.asList(userInputChararacter), (startingCharacterIndex - 1));
        for(int index = 0; index <= characterAmount - 1; index++)
            System.out.print(userInputChararacter[index]);
        /*
        for(int index = startingCharacterIndex; index <= characterAmount - 1; index++)
            System.out.print(userInputChararacter[index]);
        for(int index = 0; index <= startingCharacterIndex - 1; index++)
            System.out.print(userInputChararacter[index]);
        */

        System.out.println();
    }
}