圆形的ArrayList(扩展ArrayList的) [英] Circular ArrayList (extending ArrayList)

查看:176
本文介绍了圆形的ArrayList(扩展ArrayList的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的程序有需要的类型圆形的ArrayList的。

So my program has a need of a type of circular ArrayList.

只有圆形的东西,原来是这样:

Only circular thing about it has to be the get(int index) method, this is the original:

    /**
     * Returns the element at the specified position in this list.
     *
     * @param  index index of the element to return
     * @return the element at the specified position in this list
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */ 
    public E get(int index) {
        rangeCheck(index);

        return elementData(index);
    }

如果索引为-1应该与指数ArrayList.size()获得元素 - 1,如果索引ArrayList.size(),它应该与指数0获得元素

If index is -1 it should get the element with index ArrayList.size()-1 and if index is ArrayList.size(), it should get the element with index 0.

achieveing​​这里面来到了我的心是简单地延长从java.util包中的ArrayList,只是重写的get(INT指数),所以它不会抛出IndexOutOfBoundsException异常对上述两个指标,而是改变他们的最简单的方法就是我想。它会抛出IndexOutOfBoundsException异常对于超出范围的其他任何指标。

Simplest way of achieveing this which came to my mind is simply extending ArrayList from the java.util package and just overriding the get(int index) so it does not throw IndexOutOfBoundsException for the two indexes above, but change them to what I want. It would throw IndexOutOfBoundsException for any other index that is out of bounds.

然而,由于elementData中(索引)访问

However, since elementData(index) access a

private transient Object[] elementData;

我不能让它工作,因为我的课没有看到它,因为它是私有的。

I cannot make it work, because my class doesn't see it since it's private.

另外,我不希望使用任何外部库对于这一点,只是因为我觉得有适合我的需要,因为我不希望有一个真正的circularArray没有,而只是它的一部分的功能​​,休息它是常规的ArrayList

Also, I don't want to use any external libraries for this, simply because I think there are none that suit my needs, since I don't want a real circularArray, but only a part of it's functionality, rest of it being of the regular ArrayList.

所以,我有两个问题:

我怎样才能使这项工作?有没有办法做到这一点,而不与复制类AbstractCollection,收集和可迭代沿着整个ArrayList类到我的程序?这似乎是坏的设计,甚至在我身上。

How can I make this work? Is there a way to do it without copying the whole ArrayList class along with AbstractCollection, Collection and Iterable into my program? That seems like bad design even to me.

如果我能以某种方式让它工作,还有什么我应该看什么?如果我让上述的变化,将是改变类的行为只有我希望它的方式,或者可能会有其他任何不当行为的改变?

If I can somehow make it work, is there anything else I should watch for? If I make the changes described above, would that change the behaviour of the class only the way I want it to, or could there be any other undesired behaviour changes?

编辑:
感谢您的回答,这里是我做了什么:

Thanks for the answer, here's what I've done:

import java.util.ArrayList;

public class CircularArrayList<E> extends ArrayList<E>
{
    private static final long serialVersionUID = 1L;

    public E get(int index)
    {
        if (index == -1)
        {
            index = size()-1;
        }

        else if (index == size())
        {
            index = 0;
        }

        return super.get(index);
    }
}

这将环绕ArrayList中,但只有一个。我想它,如果我尝试访问任何其他元素,但除了他们的常规ArrayList的索引第一个和最后与任何抛出异常。

It will wrap around the ArrayList, but only by one. I want it to throw an exception if I try to access any other element but the first and the last with anything except their regular ArrayList indexes.

推荐答案

你能不能获得ArrayList中,并沿着这些线路覆盖了get(INT指数)方法:

Can't you derive from ArrayList and override the the get(int index) method along those lines:

@Override
public E get(int index)
{
    if(index < 0)
        index = index + size();

    return super.get(index);
}

我是什么失踪?

请注意,此实现不会(分别与正面和负面的指数,有点像Python)的折叠任意索引到您的有效索引范围,但只允许你正确地从左右两侧解决您的清单。

Note that this implementation would not fold arbitrary indices into your valid index range but only allow you to properly address your list from both the left and right sides (with positive and negative indices respectively, a bit like in Python).

这篇关于圆形的ArrayList(扩展ArrayList的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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