如何从 Java 中的 ArrayList 中切出 ArrayList?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1480663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:16:49  来源:igfitidea点击:

How can I slice an ArrayList out of an ArrayList in Java?

javaarraylist

提问by B T

How do I get an array slice of an ArrayListin Java? Specifically I want to do something like this:

如何ArrayList在 Java 中获取 an 的数组切片?具体来说,我想做这样的事情:

ArrayList<Integer> inputA = input.subList(0, input.size()/2);
// where 'input' is a prepouplated ArrayList<Integer>

So I expected this to work, but Java returns a List- so it's incompatible. And when I try to cast it, Java won't let me. I need an ArrayList- what can I do?

所以我希望这能工作,但 Java 返回一个List- 所以它不兼容。当我尝试投射它时,Java 不会让我这样做。我需要一个ArrayList——我能做什么?

采纳答案by Stephen C

In Java, it is good practice to use interface types rather than concrete classes in APIs.

在 Java 中,最好使用接口类型而不是 API 中的具体类。

Your problem is that you are using ArrayList(probably in lots of places) where you should really be using List. As a result you created problems for yourself with an unnecessary constraint that the list is an ArrayList.

您的问题是您正在使用ArrayList(可能在很多地方)您真正应该使用的地方List。结果,您给自己带来了一个不必要的限制,即列表是ArrayList.

This is what your code should look like:

这是你的代码应该是这样的:

List input = new ArrayList(...);

public void doSomething(List input) {
   List inputA = input.subList(0, input.size()/2);
   ...
}

this.doSomething(input);


Your proposed "solution" to the problem was/is this:

您提出的问题“解决方案”是/是这样的:

new ArrayList(input.subList(0, input.size()/2))

That works by making a copy of the sublist. It is not a slice in the normal sense. Furthermore, if the sublist is big, then making the copy will be expensive.

这通过制作子列表的副本来工作。它不是正常意义上的切片。此外,如果子列表很大,那么制作副本将会很昂贵。



If you are constrained by APIs that you cannot change, such that you have todeclare inputAas an ArrayList, you might be able to implement a custom subclass of ArrayListin which the subListmethod returns a subclass of ArrayList. However:

如果您受到无法更改的 API 的约束,以至于您必须声明inputAArrayList,则您可能能够实现一个自定义子类,ArrayList其中该subList方法返回 的子类ArrayList。然而:

  1. It would be a lot of work to design, implement and test.
  2. You have now added significant new class to your code base, possibly with dependencies on undocumented aspects (and therefore "subject to change") aspects of the ArrayListclass.
  3. You would need to change relevant places in your codebase where you are creating ArrayListinstances to create instances of your subclass instead.
  1. 设计、实施和测试将需要大量工作。
  2. 您现在已经向代码库添加了重要的新类,可能依赖于类的未记录方面(因此“可能会更改”)方面ArrayList
  3. 您需要更改代码库中创建ArrayList实例的相关位置,以创建子类的实例。

The "copy the array" solution is more practical ... bearing in mind that these are not true slices.

“复制数组”解决方案更实用......记住这些不是真正的切片。

回答by Jorge Israel Pe?a

If there is no existing method then I guess you can iterate from 0 to input.size()/2, taking each consecutive element and appending it to a new ArrayList.

如果没有现有的方法,那么我想您可以从 0 到 迭代input.size()/2,获取每个连续元素并将其附加到新的 ArrayList。

EDIT: Actually, I think you can take that List and use it to instantiate a new ArrayList using one of the ArrayList constructors.

编辑:实际上,我认为您可以使用该 List 并使用它来使用ArrayList 构造函数之一实例化一个新的 ArrayList

回答by B T

This is how I solved it. I forgot that sublist was a direct reference to the elements in the original list, so it makes sense why it wouldn't work.

我就是这样解决的。我忘记了子列表是对原始列表中元素的直接引用,所以它为什么不起作用是有道理的。

ArrayList<Integer> inputA = new ArrayList<Integer>(input.subList(0, input.size()/2));

回答by Aman Gupta

I have found a way if you know startIndex and endIndex of the elements one need to remove from ArrayList

如果您知道需要从 ArrayList 中删除的元素的 startIndex 和 endIndex,我已经找到了一种方法

Let albe the original ArrayList and startIndex,endIndexbe start and end index to be removed from the array respectively:

al是原始的 ArrayList 和startIndexendIndex分别是要从数组中删除的开始和结束索引:

al.subList(startIndex, endIndex + 1).clear();

回答by Hari Rao

Although this post is very old. In case if somebody is looking for this..

虽然这个帖子很老了。如果有人正在寻找这个..

Guava facilitates partitioning the List into sublists of a specified size

Guava 便于将 List 划分为指定大小的子列表

List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
    List<List<Integer>> subSets = Lists.partition(intList, 3);