ArrayList仅删除内容而不删除元素

时间:2014-04-14 12:59:14

标签: java android arraylist

我写过类似的代码:

ArrayList<String> someArrayList = new ArrayList<String>();
....
....
inserts elements into someArrayList
....
....
int location = 4;
someArrayList.remove(location);

它的大小为5,当我执行此操作时,它会移除位置4处的内容,但是大小不会改变,它会保持为5,当我到达第4个元素时它会显示空的。

我应该做些别的事情来彻底抹掉位置4还是什么?

修改

删除前的Logcat:

04-14 15:03:41.790: W/System.err(22971): [1, 2, 3, 4]
04-14 15:03:41.790: W/System.err(22971): [1396850896.089319.jpg
04-14 15:03:41.790: W/System.err(22971): , 1396850896.47272.png
04-14 15:03:41.790: W/System.err(22971): , 1396850897.830785.jpg
04-14 15:03:41.790: W/System.err(22971): , 1396850895.729823.jpg
04-14 15:03:41.790: W/System.err(22971): ]

完成someArrayList.remove(location);

之后的Logcat
04-14 15:03:41.790: W/System.err(22971): [1, 2, 3, 4]
04-14 15:03:41.790: W/System.err(22971): [1396850896.089319.jpg
04-14 15:03:41.790: W/System.err(22971): , 1396850896.47272.png
04-14 15:03:41.790: W/System.err(22971): , 1396850895.729823.jpg
04-14 15:03:41.790: W/System.err(22971): ]

我这样做:

// get all ids

    curFileName.moveToFirst();
    while(!curFileName.isAfterLast()) {
        ids.add(curFileName.getString(curFileName.getColumnIndex("id")));
        curFileName.moveToNext();
    }

    // get all filenames

    curFileName.moveToFirst();
    while(!curFileName.isAfterLast()) {
        filenames.add(curFileName.getString(curFileName.getColumnIndex("filename")));
        curFileName.moveToNext();
    }

然后做了一些工作后:

filenames.remove(location); // location is calculated when the work is being done

1 个答案:

答案 0 :(得分:0)

尝试

someArrayList.trimToSize();

删除元素后。