简体   繁体   English

如何让子列表引用他原来的ArrayList?

[英]How to make a sublist refers to his original ArrayList?

I have a homework to do in java about ArrayList and Generics types.我有一个关于 ArrayList 和泛型类型的 Java 作业要做。

I have 2 classes : -> CoupeDeA -> TableauPartiel我有 2 个课程:-> CoupeDeA -> TableauPartiel

CoupeDeA is just a describer from where to where an array is cut. CoupeDeA 只是一个描述从哪里到哪里切割阵列。 (It contains only two private integer variables "begin" and "end") (它只包含两个私有整数变量“begin”和“end”)

TableauPartiel is the class where the ArrayList is. TableauPartiel 是 ArrayList 所在的类。

My problem is I need to create a method in TableauPartiel like this :我的问题是我需要在 TableauPartiel 中创建一个这样的方法:

public TableauPartiel<E> coupe(CoupeDeA coupe)

And the TableauPartiel returned needs to be a reference of my intial TableauPartiel.返回的 TableauPartiel 需要是我初始 TableauPartiel 的参考。 Example :例子 :

Integer[] arr = {8,7,6,5};
TableauPartiel<E> tab = new TableauPartiel<>(arr);

TableauPartiel<E> tab2 = tab.coupe(1,3);
tab2.set(1,45);

This code is supposed to set 45 at index 1 of my tab2 and at the same time set 45 at index 2.此代码应该在我的 tab2 的索引 1 处设置 45,同时在索引 2 处设置 45。

But I tried many different ways and I managed to get the sublist, but it is not pointing to my original ArrayList.但是我尝试了很多不同的方法,并设法获得了子列表,但它没有指向我原来的 ArrayList。

For example, I tried something like this :例如,我尝试过这样的事情:

private ArrayList<E> tableau;
...
public TableauPartiel<E> coupe(Coupe coupe)
            throws IndexOutOfBoundsException {
    if (coupe.getBegin() >= 0 && coupe.getEnd() <= tableau.size()) {

        TableauPartiel<E> tab = new TableauPartiel<>((E[]) new Object[coupe.getEnd()-coupe.getBegin()]);

        for (int i = 0; i < coupe.getEnd()-coupe.getBegin(); ++i) {
            tab.set(i, this.get(coupe.getBegin()+i));
        }

        return tab;

    } else {
        throw new IndexOutOfBoundsException();
    }
}

How can I do to get a sublist which refers to his original ArrayList?我怎样才能得到一个引用他原来的 ArrayList 的子列表?

I've found a solution for my code with the subList method and by switching the signature of my ArrayList to List but my teacher doesn't want us to use subList finally.我用 subList 方法找到了我的代码的解决方案,并通过将我的 ArrayList 的签名切换到 List 但我的老师不希望我们最终使用 subList。 Here is my code with the subList method :这是我使用 subList 方法的代码:

TableauPartiel<E> tab;

if (coupe.getDebut() >= 0 && coupe.getFin() <= taille()) {
    if (coupe.getFin() == -1)
        tab = new TableauPartiel<>(tableau.subList(coupe.getDebut(),taille()));
    else
        tab = new TableauPartiel<>(tableau.subList(coupe.getDebut(),coupe.getFin()));

     return tab;
    } else {
    throw new IndexOutOfBoundsException();
    }
}

Few small things first:先说几件小事:

  • stick to English words in your code.在代码中坚持使用英语单词。 Especially in names of classes, functions, variables, etc - names have to reveal intentions (without Google Translate).特别是在类、函数、变量等的名称中 - 名称必须表明意图(没有谷歌翻译)。 Best not to obtain a bad habit by letting yourself do otherwise.最好不要让自己做其他事情,从而养成坏习惯。
  • I am not so sure how your Coupe is expected to work (is 0 a legal min number or 1 ?) but coupe.getEnd() <= tableau.size() might get out of hand我不太确定你的Coupe应该如何工作( 0是合法的最小数字还是1 ?)但是coupe.getEnd() <= tableau.size()可能会失控

Now my suggestion for the solution:现在我对解决方案的建议:

I suggest you modify your TableauPartiel class to have start and end integer fields in addition to private ArrayList<E> tableau;我建议你修改你的TableauPartiel类,除了private ArrayList<E> tableau;之外,还有startend整数字段private ArrayList<E> tableau; reference you already have.参考你已经有了。 Maybe add a new 'copy constructor' accepting an instance of TableauPartiel (from which you can copy reference to tableau ) and two int values indicating which part of the original tableau you can use (trick here is to also look at start and end values of the object you're 'sublisting' from).也许添加一个新的“复制构造函数”,它接受TableauPartiel一个实例(您可以从中复制对tableau引用)和两个int值,指示您可以使用原始tableau哪一部分(这里的技巧是还查看的startend值您从中“子列表”的对象)。 That way, when you're calling #coupe you can check for validity of the input numbers (as you already do) and simply return a new TableauPartiel object with a reference to this and method params - start and end values.这样,当您调用#coupe您可以检查输入数字的有效性(就像您已经做的那样),并简单地返回一个新的TableauPartiel对象,其中包含对this和方法参数的引用 - startend值。 Add some indexes manipulation logic using those start and end to whatever methods your TableauPartiel has and you should be good to go.使用这些startend将一些索引操作逻辑添加到您的TableauPartiel具有的任何方法中,您应该很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM