删除指定索引处的一个列表中的项目并将其添加到另一个列表中

时间:2015-10-05 09:48:01

标签: java arraylist

我有两个ArrayList s:

ArrayList a = [1,2,3,4,5,6,7,8,9,10,11.......100]
ArrayList b = [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10.......a100]

删除ArrayList的每个第5和第6个元素,例如5,6 11,12,依此类推,并移至ArrayList b。

并删除ArrayList b的每个第6和第7项,如a6,a7,a13,a14等,并将其添加到ArrayList a。

所以我的输出ArrayList就是。

 ArrayList a = [1,2,3,4,a6,a7,7,8,9,10,a13,a14,13,14,15,16,a20,a21 and so on]
 ArrayList b = [a1,a2,a3,a4,a5,5,6,a8,a9,a10,a11,a12,11,12 ,a15,a16,a17,a18,a19,17,18and so on]

那我怎么能实现这个目标呢?

我尝试了但不是以确切的方式。

       int highAdded = 0;
    int normalAdded = 0;
    for (Iterator<BaseItem> iterator = mItems.iterator(); iterator.hasNext(); ) {
        BaseItem itemtype = iterator.next();
        if (itemtype.isHighlightPost()) {

            highAdded++;
            if (highAdded == 5) {
                normal.add(itemtype);
            } else if (highAdded == 6) {
                normal.add(itemtype);
                highAdded = 0;
            } else {
                highlighted.add(itemtype);
            }
        } else {
            normalAdded++;
            if (normalAdded == 6) {
                highlighted.add(itemtype);
            } else if (normalAdded == 7) {
                highlighted.add(itemtype);
                normalAdded = 0;
            } else {
                normal.add(itemtype);
            }
        }

    }

由于

2 个答案:

答案 0 :(得分:2)

这应该做的工作,注意这里没有删除,但它确实替换。确保设置N以便不超过列表大小。

/** Multiplier for a-list index */
private final static int M_A = 6;
/** Multiplier for b-list index */
private final static int M_B = 7;

{
   // ...
   final int n = Math.min( a.size() / M_A, b.size() / M_B );
   for ( int i = 1; i <= n; i++ ) {
      exchange( a, b, i * M_A - 2, i * M_B - 2 );
      exchange( a, b, i * M_A - 1, i * M_B - 1 );
   }
   // ...
}

private <T> void exchange( List<T> a, List<T> b, int aIndex, int bIndex ) {
   T elem1 = a.get( aIndex );
   a.set( aIndex, b.get( bIndex ) );
   b.set( bIndex, elem1 );
}

(编辑以匹配索引计数器的'规范') (编辑2,还添加了n的计算)

答案 1 :(得分:1)

您可以通过arraylist的set()方法完成此操作

public void arraylistproblem()
{
    ArrayList<String> a=new ArrayList<>();
    for(int i=1;i<=100;i++)
    {
        a.add(""+i);
    }

    ArrayList<String> b=new ArrayList<>();
    for(int j=1;j<=100;j++)
    {
        b.add("a"+j);
    }

    System.out.print("Arraylist a = ");
    for(int i1=0;i1<a.size();i1++)
    {
        System.out.print(a.get(i1)+",");
    }
    System.out.print("Arraylist b = ");
    for(int i1=0;i1<b.size();i1++)
    {
        System.out.print(b.get(i1) + ",");
    }

    int aIndex=4;
    int bIndex=5;
    for(int i=0;i<a.size();i++)
    {

        if(aIndex>=a.size() || bIndex>=b.size())
            break;

        String aTemp1=a.get(aIndex);
        String bTemp1=b.get(bIndex);

        /**
         * swap the values .. 5th of a-arraylist and 6th of b-arraylist
         */
        a.set(aIndex++, bTemp1);
        b.set(bIndex++, aTemp1);

        String aTemp2=a.get(aIndex);
        String bTemp2=b.get(bIndex);

        /**
         * swap the values .. 6th of a-arraylist and 7th of b-arraylist
         */
        a.set(aIndex, bTemp2);
        b.set(bIndex, aTemp2);

        aIndex=aIndex+5;
        bIndex=bIndex+6;
    }

    System.out.print("Arraylist a = ");
    for(int i1=0;i1<a.size();i1++)
    {
        System.out.print(a.get(i1)+",");
    }
    System.out.print("Arraylist b = ");
    for(int i1=0;i1<b.size();i1++)
    {
        System.out.print(b.get(i1)+",");
    }
}