将 `ArrayList` 的内容向右移动 [英] Shifting the contents of the `ArrayList` to the right

查看:35
本文介绍了将 `ArrayList` 的内容向右移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一段代码,将 ArrayList 的内容向右移动,移动可以是传递给方法的任何数字shiftRight(String myString, int shift)在方法内部,我需要将传入的String 的每个char 放入ArrayList myList.

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.

例如,如果我有

"abcdef", 2

结果应该是

efabcd

因为它将 2 个字母向右移动.另一个例子:

as it shifts 2 letters to the right. Another example:

"abcdef", 4

那么输出是

cdefab

当移位为 2String"abcdef" 时,我的代码给出 edabcf 而它应该产生 efabcd.Coulb smb 请帮帮我?我试图调试它,但仍然无法弄清楚为什么它采用 d 而不是 f.提前致谢!

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!

代码逻辑:

1) 通过运行 for - loop

2) 我将 shift 的值分配给一个临时的 count,它在 while-loop 内递减.在 while-loop 中,我添加了即将被删除的字符到 myList 中的 ArrayList temp(如果 shift2,然后字符 ef 添加到 temp).之后在同一循环中从 myList 中删除这些字符.

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) 将 temp 列表中的字符添加到 myList

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

推荐答案

正如 jdevelop 的回答所述,最好的方法是实现一个对 ArrayList 进行操作的简单方法.

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.

然后在列表上使用 set(int index, T t) 方法重新引入粉红色元素.

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

在数组列表中,这并没有太大的不同.通常也更喜欢使用 List over an ArrayList,不过这是主观的.

In an arraylist, this is not much different. It is also generally preferred to use a List over an ArrayList, though this is subjective.

这篇关于将 `ArrayList` 的内容向右移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆