【问题标题】:Why is there no such removeRange() method in CopyOnWriteArrayList?为什么 CopyOnWriteArrayList 中没有这样的 removeRange() 方法?
【发布时间】:2013-07-27 13:49:33
【问题描述】:

为什么在ArrayList中有这样的方法而在并发兄弟上却没有?

受保护的 void removeRange(int fromIndex, int toIndex)

只是好奇,这不是根本问题,我可以解决它。

【问题讨论】:

  • 你能举一个 List 的例子吗?
  • @PeterLawrey ArrayList 的 List 实现具有该方法。我只是好奇为什么它在并发版本中不一样..可能是由于一些同步问题?我知道如何解决它。谢谢
  • 如果对这件事感兴趣,请查看:stackoverflow.com/questions/2289183/…

标签: java arrays concurrency copyonwritearraylist


【解决方案1】:

你可以间接做到这一点。

List<Integer> ints = new CopyOnWriteArrayList<Integer>();
for (int i = 0; i < 10; i++) ints.add(i);
ints.subList(4, 7).clear();
System.out.println(ints);

打印

[0, 1, 2, 3, 7, 8, 9]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 2011-04-09
    相关资源
    最近更新 更多