问题
I've written a code which shifts the contents of the ArrayList
to the right and the shifting can be any number passed to the method
shiftRight(String myString, int shift)
Inside the method, I need to put every char
of a passed String
into the ArrayList myList
.
For example, if I have
"abcdef", 2
the result should be
efabcd
as it shifts 2
letters to the right.
Another example:
"abcdef", 4
then the output is
cdefab
My code gives edabcf
when the shift is 2
and the String
is "abcdef"
whereas it should produce efabcd
. Coulb smb please help me out? I tried to debug it but still could not figure out why it's taking d
instead of f
. Thanks in advance!
Logic of the code:
1)Put every character inside the ArrayList myList
by running a for - loop
2)I assigned the value of shift
to a temporary count
which decrements inside the while-loop
. Inside the while-loop
I added the character, which would be removed soon, to the ArrayList temp
from the myList
(if the shift
is 2
, then the characters ef
are added to the temp
). Remove those characters from myList
afterwards inside the same loop.
3)Added characters from temp
list to the myList
import java.util.ArrayList;
public class ShiftToTheRight {
public static void main(String[] args){
String myString = "abcdef";
int shift = 2;
ArrayList<String> myList = shiftRight(myString, shift);
for(int i = 0; i < myList.size(); i++){
System.out.print(myList.get(i) + "");
}
}
public static ArrayList<String> shiftRight(String myString, int shift){
ArrayList<String> myList = new ArrayList<>();
//Put every character inside the myList
for(int i = 0; i < myString.length(); i++){
myList.add(myString.charAt(i) + "");
}
ArrayList<String> temp = new ArrayList<>();
//Add the rightmost characters into the temp
//Delete those characters from the myList
int count = shift;
while(count != 0){
temp.add(myList.get(myList.size() - shift));
myList.remove(myList.get(myList.size() - shift));
count--;
}
//Add the characters from the temp to the beginning of the myList
for(int i = 0; i < temp.size(); i++){
myList.add(i ,temp.get(i));
}
return myList;
}
}
回答1:
As jdevelop's answer states, the best way to do this is to implement a simple method that operates on the ArrayList.
As I cannot add a picture to my comments without simply providing a link, I'm posting this answer in support of his.
In the first step of his code, a copy of the last element (the pink oval) is copied for later. All elements are shifted to the right.
Then the pink element is reintroduced using the set(int index, T t) method on lists.
The method you would incorporate to do this multiple times would look similar to this, including his original code:
void shiftArrayXTimes(List<T> l,int x){
while(int i = 0; i < x; i++){
shiftRight(l);
}
}
In an arraylist, this is not much different. It is also generally preferred to use a List over an ArrayList, though this is subjective.
回答2:
It is not required to use a List-class for that simple string-operations. it can be done using substring:
private static String shift(String input, int count){
if(count >= input.length())
throw new IllegalArgumentException("count should be smaller than input.length");
int start = input.length()-count;
String part1 = input.substring(start);
return part1 + input.substring(0, start);
}
public static void main(String[] args){
System.out.println(shift("abcdef", 2));
System.out.println(shift("abcdef", 4));
}
Output is:
efabcd
cdefab
回答3:
What you have to do for the shift is to simply add the single function that takes the single value from the end of the list, and moves things one step to the right.
public static <T> void shiftRight(List<T> lst) {
if (lst == null || lst.isEmpty()) {
return;
}
T t = lst.get(lst.size() - 1);
// list index is zero-based
for (int i = lst.size() - 2; i >= 0; i--) {
lst.set(i + 1, lst.get(i));
}
lst.set(0, t);
}
Hence you're doing shift by 1 item to the right, and you use O(1) of the additional memory to keep the new list.
now if you need to shift by 2 items - invoke the function twice.
回答4:
Instead of mutating a list, this problem would be easier to solve using an array. An ArrayList has an array as a backing store but it obfuscates the issue at hand which is manipulating the char indices.
Try this instead:
public static String shiftRight(final String theString, final int theShift){
char[] string = theString.toCharArray();
char[] temp = string.clone();
int indexRun = 0;
for (int i = theShift; i < string.length; i++) {
temp[i] = string[indexRun];
indexRun++;
}
for (int i = 0; i < theShift; i++) {
temp[i] = string[indexRun];
indexRun++;
}
return new String(temp);
}
回答5:
Instead of myList.size() - shift
do myList.size() - 1
. You wanna grab the last char. Same with the remove. And when putting together the chars, you wanna do myList.add(0, temp.get(i)
so it always adds the chars to the beginning of the list. Otherwise, your code seems functional
来源:https://stackoverflow.com/questions/31976100/shifting-the-contents-of-the-arraylist-to-the-right