将元素从一个ArrayList移动​​到另一个ArrayList

时间:2015-01-09 01:48:49

标签: java arraylist

我的课程中的指示是:完成下面的静态方法migrateBlock的定义,对于任何非负整数n,从索引start处的元素开始移除ArrayList fromList中的n个元素块并插入它们(按顺序)从索引目标开始的ArrayList toList。 (如果从索引开始到fromList结尾的元素中少于n个元素,则该方法将ArrayList的整个" tail"从具有索引start的元素移植到结尾。)

到目前为止,我有这个:

public static void transplantBlock( ArrayList<String> fromList,
                                    ArrayList<String> toList,
                                    int start,
                                    int target,
                                    int n ) {
    for ( int i = 0; i < n; i++) {
        if ( fromList.size() > start) {
            fromList.remove(start);            
        }  
    }
}

public static void main( String[] args )
{
  String[] fromData = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k" };
  ArrayList<String> fromList = new ArrayList<String>();
  for ( int i = 0; i < fromData.length; i++ )
    fromList.add( fromData[ i ] );

  String[] toData = { "z", "y", "x", "w", "v", "u", "t", "s" };
  ArrayList<String> toList = new ArrayList<String>();
  for ( int i = 0; i < toData.length; i++ )
    toList.add( toData[ i ] );

  transplantBlock( fromList, toList, 6, 3, 4 );

  System.out.println( "After:" );
  System.out.println( "   fromList: " + fromList );
  System.out.println( "   toList: " + toList );
}

输出应为:

After:
   fromList: [a, b, c, d, e, f, k]
   toList: [z, y, x, g, h, i, j, w, v, u, t, s]

我能够从fromList中删除正确的元素,但我无法弄清楚如何将它们添加到toList。如果有人能帮助我弄清楚我错过了什么,我会非常感激。谢谢!

(旁注:这是填空的作业,所以这不是课堂问题)

3 个答案:

答案 0 :(得分:1)

跟踪索引插入刚刚删除的元素的位置:

int insertIndex = target;
for (int i = 0; i < n; i++) {
    if (fromList.size() > start) {
        String removed = fromList.remove(start);
        toList.add(insertIndex, removed);
        insertIndex++;
    }
}

答案 1 :(得分:0)

add(int index, E element)

中有一个List方法

它应该类似于:

ArrayList<String> myList = new ArrayList(Arrays.asList("a","b","c","d"));

myList.add(2, "x");

// List should look like [a, b, x, c, d] now

其余的应该是微不足道的。

答案 2 :(得分:0)

public static void transplantBlock( ArrayList<String> fromList,
                                    ArrayList<String> toList,
                                    int start,
                                    int target,
                                    int n )


   {
    for (int i = 0; i < n; i++)
     {
        if (fromList.size() > start)
         {
            String removed = fromList.remove(start);
            toList.add(target, removed);
            target++;
        }
    }
   }


 public static void main( String[] args )
{
  String[] fromData = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k" };
  ArrayList<String> fromList = new ArrayList<String>();
  for ( int i = 0; i < fromData.length; i++ )
    fromList.add( fromData[ i ] );

  String[] toData = { "z", "y", "x", "w", "v", "u", "t", "s" };
  ArrayList<String> toList = new ArrayList<String>();
  for ( int i = 0; i < toData.length; i++ )
    toList.add( toData[ i ] );

  transplantBlock( fromList, toList, 6, 3, 4 );

  System.out.println( "After:" );
  System.out.println( "   fromList: " + fromList );
  System.out.println( "   toList: " + toList );
}