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

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

问题描述

我写了一个code这转移了的ArrayList 的权利和换档可任意数目的传递给方法的内容
shiftRight(字符串MyString的,诠释移)
里面的方法,我需要把每一个字符的传递字符串的ArrayList myList中

例如,如果我有

 ABCDEF,2

结果应该是

  efabcd

因为它转移 2 信件的权利。
又如:

 ABCDEF,4

,则输出是

  cdefab

我的code给出 edabcf 时移 2 字符串ABCDEF,而它应该产生 efabcd 。 Coulb SMB请帮助我吗?我试图调试它,但仍然无法弄清楚,为什么它采取 D 而不是˚F。在此先感谢!


在code的逻辑:

1)把里面的每一个人物的的ArrayList myList中通过运行为 - 环

2)给我分配的值来临时计数这递减 while循环。在 while循环我添加了角色,这将很快被移除,到 ArrayList的温度 myList中(如 2 ,那么字符 EF 添加到温度)。从 myList中删除这些字符之后里面同一个循环。

3)添加的字符从温度列表的 myList中


 进口的java.util.ArrayList;
公共类ShiftToTheRight {    公共静态无效的主要(字串[] args){        字符串的myString =ABCDEF;
        INT移= 2;
        ArrayList的<串GT; myList中= shiftRight(MyString的,移);
        的for(int i = 0; I< myList.size();我++){
            System.out.print(myList.get(I)+);
        }
    }    公共静态的ArrayList<串GT; shiftRight(字符串MyString的,诠释移){        ArrayList的<串GT; myList中=新的ArrayList<>();        //把每一个角色的myList中内
        的for(int i = 0; I< myString.length();我++){
            myList.add(myString.charAt(I)+);
        }        ArrayList的<串GT;临时=新的ArrayList<>();        //最右边的字符添加到临时
        //从myList中删除这些字符
        诠释计数=转变;
        而(计数!= 0){
            temp.add(myList.get(myList.size() - 移));
            myList.remove(myList.get(myList.size() - 移));
            计数 - ;
        }        //从临时添加字符到myList中的开头
        的for(int i = 0; I< temp.size();我++){
            myList.add(ⅰ,temp.get(ⅰ));
        }
        返回myList中;
    }
}


解决方案

随着jdevelop的回答状态,要做到这一点的最好办法是实施对ArrayList的操作的简单方法。

由于我不能没有简单地提供一个链接,我支持他的张贴这个答案图片添加到我的意见。

在这里输入的形象描述

在他的code的第一步,最后一个元素(粉红色椭圆形)的副本复制供以后使用。所有的元素都向右移动

然后粉色元素是使用集(INT指数,T T)上列出的方法重新提出。

该方法,你将包括做这多次将类似于此,包括他原来的code:

 无效shiftArrayXTimes(列表< T> L,INT X){
    而(INT I = 0; I< X,我++){
        shiftRight(升);
}
}

在一个ArrayList,这是没有太大的不同。人们还普遍$ pferred了一个ArrayList 列表P $ >,虽然这是主观的。

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

解决方案

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.

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

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