我已经编写了一个代码,可以将ArrayList
的内容向右移动,并且移位可以是传递给方法的任何数字
shiftRight(String myString, int shift)
在方法内部,我需要将char
的每个String
放入ArrayList myList
。
例如,如果我有
"abcdef", 2
结果应该是
efabcd
因为它将2
个字母向右移动。
另一个例子:
"abcdef", 4
然后输出
cdefab
当代码为edabcf
且2
为String
时,我的代码会提供"abcdef"
,而应生成efabcd
。 Coulb smb请帮帮我?我试图调试它,但仍然无法弄清楚为什么它采用d
代替f
。提前谢谢!
代码逻辑:
1)通过运行ArrayList myList
for - loop
内
2)我将shift
的值分配给临时count
,其在while-loop
内递减。在while-loop
内,我将ArrayList temp
中的myList
添加到即将被删除的字符(如果shift
为2
,则字符ef
已添加temp
。之后在同一循环内从myList
删除这些字符。
3)将temp
列表中的字符添加到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;
}
}
答案 0 :(得分:0)
正如jdevelop的回答所述,最好的方法是实现一个在ArrayList上运行的简单方法。
由于我无法在不提供链接的情况下在我的评论中添加图片,因此我发布此答案以支持他的评论。
在他的代码的第一步中,复制最后一个元素(粉色椭圆形)的副本供以后使用。所有元素都向右移动。
然后使用列表上的set(int index,T t)方法重新引入pink元素。
多次执行此操作所包含的方法看起来与此类似,包括原始代码:
void shiftArrayXTimes(List<T> l,int x){
while(int i = 0; i < x; i++){
shiftRight(l);
}
}
在一个arraylist中,这并没有太大的不同。通常也优选使用List over an ArrayList,尽管这是主观的。
答案 1 :(得分:-1)
你需要做的就是简单地添加从列表末尾获取单个值的单个函数,并向右移动一步。
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);
}
因此,您正在向右移动1个项目,并使用额外内存的O(1)来保留新列表。
现在如果您需要换班2件 - 调用该功能两次。
答案 2 :(得分:-1)
对于那个简单的字符串操作,不需要使用List-class。 它可以使用子字符串完成:
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));
}
输出是:
efabcd
cdefab
答案 3 :(得分:-1)
使用数组更容易解决这个问题,而不是改变列表。 ArrayList有一个数组作为后备存储,但它混淆了正在操纵char索引的问题。
请改为尝试:
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);
}
答案 4 :(得分:-1)
而不是myList.size() - shift
做myList.size() - 1
。你想抓住最后一个字符。与删除相同。把这些字符放在一起时,你想做myList.add(0, temp.get(i)
所以它总是将字符添加到列表的开头。否则,您的代码似乎功能正常