如何从 Java 中的 ArrayList 中切出 ArrayList? [英] How can I slice an ArrayList out of an ArrayList in Java?

查看:30
本文介绍了如何从 Java 中的 ArrayList 中切出 ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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 中,最好使用接口类型而不是 API 中的具体类.

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

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

Your problem is that you1 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.

您的代码应该是这样的:

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);


1 - 根据您的评论,您"实际上是其他人......在面试问题中设置了这个问题.这可能实际上是一个技巧问题,旨在了解您将如何处理创建与 ArrayList 兼容的赋值的 ArrayList 的(真实)切片.


1 - Based on your comments, "you" was actually someone else ... who set this problem in an interview question. It is possible that this was actually a trick question, designed to see how you would cope with creating a (real) slice of an ArrayList that was a assignment compatible with ArrayList.

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

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.

如果您受到无法更改的 API 的限制,则您必须inputA 声明为 ArrayList,您也许能够实现 ArrayList 的自定义子类,其中 subList 方法返回 ArrayList 的子类.然而:

If you are constrained by APIs that you cannot change, such that you have to declare inputA as an ArrayList, you might be able to implement a custom subclass of ArrayList in which the subList method returns a subclass of ArrayList. However:

  1. 设计、实施和测试需要做大量工作.
  2. 您现在已经向代码库添加了重要的新类,可能依赖于 ArrayList 类的未记录方面(因此可能会发生变化").
  3. 您需要更改代码库中创建 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 ArrayList class.
  3. You would need to change relevant places in your codebase where you are creating ArrayList instances to create instances of your subclass instead.

复制数组"解决方案更实用……请记住,这些不是真正的切片.

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

这篇关于如何从 Java 中的 ArrayList 中切出 ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆