java - 是否有可能实现类似于 subList(a,b) 的方法,但当 a>b 时该方法有效?

标签 java data-structures collections

我想实现一个类似于 subList(a,b) 的方法,但它在 a>b 时有效。 subList(a,b) 和 subList(b,a) 应该返回相同范围的 ListView ,但是迭代和编号不同。在 a>b 的情况下, View 应该反转。可能吗?

我目前的解决方案非常原始。第一个问题是 subList(a,b) 在 a>b 的情况下没有相应地调整编号(对于 removeget 方法的使用)。但更重要的是,反转的 ListView 实际上是这里的副本而不是实际 View ,我什至不知道如何解决这个问题。

@SuppressWarnings("serial")
class ReverseLinkedList <T> extends LinkedList<T>
{
    ReverseLinkedList(final List<T> l)
    {
        super(l); // problem, I want a view not a copy
    }
    @Override
    public Iterator<T> iterator()
    {
        return new Iterator<T>()
        {
            ListIterator<T> listIter = listIterator(size());
            public boolean hasNext() 
            { 
                return listIter.hasPrevious(); 
            }
            public T next() 
            { 
                return listIter.previous(); 
            }
            public void remove() 
            { 
                listIter.remove(); 
            }  
        };
    }
}

@SuppressWarnings("serial")
class CleverList<T> extends LinkedList<T>
{
    @Override
    public List<T> subList(int fromIndex, int toIndex)
    {
        if ( fromIndex < toIndex )
        {
            return super.subList(fromIndex, toIndex);
        }
        else
        {
            return new ReverseLinkedList<T>(super.subList(toIndex-1,fromIndex-1));
        }
    }
}

目前它是如何工作的:

    CleverList<Integer> list = new CleverList<Integer>();
    for ( int i=1; i<=10; ++i )
    {
        list.add(i);
    }
    List<Integer> listA = list.subList(2,8);
    printList(listA);
    // "3 4 5 6 7 8 " ok
    List<Integer> listB = list.subList(8,2);
    printList(listB);
    // "7 6 5 4 3 2 " ok

    listB.remove(2);
    printList(listB);
    // "7 6 5 3 2 " not ok, the point was to remove "5"
    printList(list);
    // "1 2 3 4 5 6 7 8 9 10 " not ok, nothing was removed

最佳答案

一个可能的解决方案是使用组合而不是继承。它可能看起来像这样:

class ReverseLinkedList<T> implements List<T> {
    // A reference to the actual list.
    private final List<T> list;

    public ReverseLinkedList(final List<T> list) {
        // Does not create a copy.
        // Stores a reference to the original list instead.
        this.list = list;
    }

    @Override 
    public T get(int index) {
        // Adjusts the index and calls the method on an actual list.
        return list.get(list.size() - 1 - index);
    }

    //And so on for all the other methods declared in the List interface...

}

它完全满足您的需求:它不会创建传递给构造函数的列表的副本,您可以控制所有方法(get、remove 等)来正确调整索引。这种方法的缺点是需要编写的代码量很大:List 接口(interface)中声明的每个方法都必须在 ReverseLinkedList 类中定义。

关于java - 是否有可能实现类似于 subList(a,b) 的方法,但当 a>b 时该方法有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27608244/

相关文章:

java - 二维数组的自然连接

java - Spring 和 ExtJS "400 Bad Request"使用 PUT 但不使用 POST

java - JAVA中链表转换为循环链表

java - 搜索列表中的元素

java - 通过 Stream API 从 Map 获取排序列表

java - 从 Java 集合中检索较晚的日期

java - 使用 JSTL 格式化数字且不进行舍入

java - 我可以在运行时创建 servlet 吗?

java - 将一串字符分解为有效的单词

python - Python 如何在内部存储列表?