java中如何不使用sublist方法获取子列表

发布于 2024-09-26 06:38:27 字数 331 浏览 8 评论 0原文

这就是我现在所拥有的:

public ArrayList subList(int fromIndex, int toIndex){
      ArrayList a = new ArrayList();
      for (int i=fromIndex;i<toIndex;i++) {
          a.add(stuff[i]); //stuff is a array of strings
      }
    return list;
  }

但是是否可以在不创建新数组的情况下返回子列表?我被限制使用 Array/ArrayList 类中的任何方法。

This is what I have right now:

public ArrayList subList(int fromIndex, int toIndex){
      ArrayList a = new ArrayList();
      for (int i=fromIndex;i<toIndex;i++) {
          a.add(stuff[i]); //stuff is a array of strings
      }
    return list;
  }

But is it possible to return the sublist without creating a new array? I am restrict from using any methods from the Array/ArrayList class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

提赋 2024-10-03 06:38:27

如果您希望具有与 Java subList 方法相同的行为,您需要保留指向原始列表的指针,并使用偏移量和长度来索引原始列表。

下面开始展示 get 方法的实现。

public class SubList extends AbstractList {
    private final List original;
    private final int from;
    private final int to;
    public SubList(List original, int from, int to) {
        this.original = original;
        this.from = from;
        this.to = to;
    }

    public Object get(int i) {
        if (i < 0 || i > to - from) {
            throw new IllegalArguementException();
        }

        return original.get(from + i);
    }
}

public static List subList(List original, int from, int to) {
    return new SubList(original, from, to);
}

If you want have the same behaviour as the Java subList method you need to retain a pointer to the original list and use an offset and length to index into the original list.

Heres a start showing the implementation of the get method.

public class SubList extends AbstractList {
    private final List original;
    private final int from;
    private final int to;
    public SubList(List original, int from, int to) {
        this.original = original;
        this.from = from;
        this.to = to;
    }

    public Object get(int i) {
        if (i < 0 || i > to - from) {
            throw new IllegalArguementException();
        }

        return original.get(from + i);
    }
}

public static List subList(List original, int from, int to) {
    return new SubList(original, from, to);
}
千笙结 2024-10-03 06:38:27

为了避免创建新的存储列表,您必须传入对原始列表的引用,保留子列表,然后从列表中删除剩余的项目,但这会使列表丢失那些其他项目。

如果这不是您的目标,您将不得不在某个时候创建​​一个新列表来保存子列表。

To avoid creating a new list for storage, you would have to pass in a reference to the original list, keep the sublist, and then delete the remaining items from from the list, but this would leave the list missing those other items.

If that isn't your goal you will have to create a new list at some point to hold the sublist.

奢望 2024-10-03 06:38:27

我假设您必须返回标准 ArrayList,而不是您自己的 ArrayList 版本,并且我假设“stuff”是一个数组,而不是一个列表。

首先,使 ArrayList 具有数组的初始大小(toIndex - fromIndex)可以获得奖励积分。为了获得更多奖励积分,请确保往返的不雅行为确实存在于“东西”中,否则您会遇到严重的崩溃。

ArrayList 使用内部数组进行存储,并且您无法更改它,因此您别无选择,只能创建一个副本。

编辑
您可以使事情变得有趣并且更加复杂,但这会给某人留下深刻的印象...通过创建您自己的实现 List 的 ArrayList 类来做到这一点。让它使用原始数组。非常不稳定,因为如果该数组在外部其他地方被修改,你就会遇到麻烦,但它可能很有趣。

I assume you have to return the standard ArrayList, and not your own version of ArrayList, and I assume that 'stuff' is an array, not a list.

First off, get bonus points for making the ArrayList have the initial size of the array (toIndex - fromIndex). For more bonus points, make sure that the to and from indecies actually exist in 'stuff' otherwise you get a nice crash.

ArrayList uses an internal array for its storage and you can't change that so you have no choice but to create a copy.

EDIT
You could make things interested and much more complex but it'll impress someone... Do it by creating your own ArrayList class implementing List. Get it to use that original array. Pretty unstable since if that array is modified somewhere else externally, you're in trouble, but it could be fun.

森末i 2024-10-03 06:38:27

您可以退回三件明智的事情。数组、列表或迭代器。如果我关于您应该重新实现 subList 的假设是正确的,那么就没有办法创建新的 ArrayList。

There's three sensible things you could return. An array, a List, or an Iterator. If my assumption that you're supposed to re-implement subList was correct, then there's no way around creating the new ArrayList.

毁梦 2024-10-03 06:38:27

子列表是“一个新列表”,因此您必须创建一些东西来表示数组的子列表。这可以是一个新数组或一个列表。你选择了一个对我来说看起来不错的ArrayList。您没有(直接)创建一个新数组,所以我实际上不明白您的问题。 (如果您想避免通过 ArrayList间接创建新数组,请选择另一个 List 实现,例如 LinkedList

如果您正在寻找细微改进:

  • 考虑将源数组作为方法参数传递。现在 stuff[] 是一个静态字段。
  • 考虑使用子列表的大小初始化新的 ArrayList (toList-fromList+1)
  • 考虑使用泛型(仅当您现在已经有了这个概念时)。因此返回类型将是 ArrayList

A sublist is "a new list", so you'll have to create something to represent the sublist of the array. This can either be a new array or a list. You chose an ArrayList which looks good to me. You're not creating a new array (directly), so I don't actually get that point of your question. (If you want to avoid creating a new array indirectly through ArrayList, choose another List implementation, LinkedListfor example)

If you're looking for slight improvements:

  • Consider passing the source array as a method parameter. Now stuff[] is a static field.
  • Consider initializing the new ArrayList with the size of the sublist (toList-fromList+1)
  • Consider using generics (only if you already now this concept). So the return type would be ArrayList<String>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文