Collections.rotateを動作させることができません。 userInputCharactersにqwertyを入力してこのコードを実行すると、再びQWERTYが出力されます。どんな勧告?Collections.rotate not working
Collections.rotate(Arrays.asList(userInputChararacter), 2);
for(int index = 0; index <= characterAmount - 1; index++)
System.out.print(userInputChararacter[index]);
更新:ここに私の完全なプログラムがあります。
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();
}
}
[MCVE]にこれを肉付けしてください。 –
これはあなたのために役立ちますhttp://www.geeksforgeeks.org/java-util-collections-rotate-method-java-examples/ –
それを信じるかどうか、私は実際にリンクを既に使用しています。 –