制作一个自己保持相反顺序的列表
我的任务是解决以下问题:创建一个将实现 List 的集合 ReverseList。通过使用 for 循环 (for(E e:list)) 迭代 ReverseList 类型的对象列表,我们将按照与输入顺序相反的顺序获取项目。 在从 ArrayList 扩展时实现以下类,
所以本质上我需要创建一个不遵循插入自然顺序的集合 让我澄清一下,我不想在创建列表后反转列表并使用 Collections.reverse() 之类的内容添加项目,而是让列表保持自己的顺序,
到目前为止我尝试过的是制作自定义迭代器。但是由于某种原因,当尝试迭代列表时,我被 IndexOutOfBoundsException 抛出(即使列表不为空) 我的代码:
public class ReverseList<E> extends ArrayList<E> implements List<E>{
private class ReverseIterator<E> extends ReverseList<E> implements Iterator<E>
{
private int pos;
public ReverseIterator()
{
pos = super.size()-1;
}
public ReverseIterator(ReverseList<E> r)
{
pos = r.size()-1;
}
@Override
public boolean hasNext() {
return pos >= 0;
}
@Override
public E next() {
return super.get(pos--);
}
}
@Override
public Iterator<E> iterator() {
// TODO Auto-generated method stub
return new ReverseIterator<E>(this);
}
public static void main(String[] args)
{
ReverseList<Integer> r = new ReverseList<>();
r.add(new Integer(1));
r.add(new Integer(2));
r.add(new Integer(3));
r.add(new Integer(4));
for(Integer i:r)
{
System.out.println(i);
}
}
}
抛出错误:线程“main”java.lang.IndexOutOfBoundsException中的异常:索引3超出长度0的范围(在for循环中抛出)
为什么列表的长度为0?
我的方法可行吗?有更好的方法吗?
I've been tasked with the following question: create a collection ReverseList that would implement List. by iterating over an object list of type ReverseList with a for loop (for(E e:list)) we would get the items in an order reversed of what they were entered.
implement the following class while extending from ArrayList
so essentially I need to create a collection that doesnt follow the natural ordering of insertion
let me clarify that i am not looking to reverse the list after its creation and adding items with something like Collections.reverse() but rather have the list maintain its own order
what I've tried so far is making a custom Iterator. however for some reason when trying to iterate over the list im getting thrown out with an IndexOutOfBoundsException (even though the list isnt empty)
my code:
public class ReverseList<E> extends ArrayList<E> implements List<E>{
private class ReverseIterator<E> extends ReverseList<E> implements Iterator<E>
{
private int pos;
public ReverseIterator()
{
pos = super.size()-1;
}
public ReverseIterator(ReverseList<E> r)
{
pos = r.size()-1;
}
@Override
public boolean hasNext() {
return pos >= 0;
}
@Override
public E next() {
return super.get(pos--);
}
}
@Override
public Iterator<E> iterator() {
// TODO Auto-generated method stub
return new ReverseIterator<E>(this);
}
public static void main(String[] args)
{
ReverseList<Integer> r = new ReverseList<>();
r.add(new Integer(1));
r.add(new Integer(2));
r.add(new Integer(3));
r.add(new Integer(4));
for(Integer i:r)
{
System.out.println(i);
}
}
}
error thrown: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 0 ( thrown at the for loop )
why is the list at length 0?
is my approach even possible? is there a better way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
ReverseIterator
是ReverseList
的子类。这意味着,它本身就是一个列表。然后,您混淆了这两个列表的状态。在ReverseIterator(ReverseListr)
中,您在next()< 中使用
r
的大小来初始化pos
/code> 您使用super.get(pos--)
访问另一个列表的内容。其他列表始终为空。迭代器永远不应该是集合。当您将迭代器实现为内部类时,您可以隐式访问外部集合的状态。
除此之外,您的列表显然违反了
List
接口的约定,并且将来会导致很多其他问题,因为它的iterator()
与其他不一致>List
功能,如所有基于索引的操作或listIterator()
。您不应该仅仅为了单个操作(即向后迭代)而更改类的基本原理。相反,将此单个操作实现为不同的操作。
例如:
reverse()
视图没有自己的存储空间,但始终以相反的顺序反映列表的当前内容。原来的List
继续履行它的契约。请注意,可以创建列表的反向视图,支持除
iterator()
之外的List
接口的其他操作:Your
ReverseIterator
is a subclass ofReverseList
. This means, it is a list on its own. Then, you are mixing up the state of these two lists. In theReverseIterator(ReverseList<E> r)
, you user
’s size to initializepos
, innext()
you usesuper.get(pos--)
, accessing the other list’s content. This other list is always empty.An iterator should never be a collection. When you implement an iterator as an inner class, you can access the outer collection’s state implicitly.
Besides that, your list clearly violates the contract of the
List
interface and will cause a lot of other problems in the future, as itsiterator()
is inconsistent with otherList
features, like all index based operations orlistIterator()
.You should not change the fundamentals of a class, just for the sake of a single operation (i.e. iterate backwards). Rather, implement this single operation as a distinct operation.
For example:
The
reverse()
view has no storage of its own but always reflects the current contents of the list, in reverse order. The originalList
keeps fulfilling its contract.Note that it is possible to create reversed view to a list supporting other operations of the
List
interface beyonditerator()
:自然顺序与插入顺序
这是一个矛盾的术语。自然排序不是插入顺序。
compareTo
比较
接口。
列表#reversed
因此以正常方向构建列表。之后,在 Java 21+ 中,只需调用
List#reversed
获取该列表的视图,该视图呈现与默认遭遇顺序相反的遭遇顺序。运行时:
要了解更多信息,请参阅JEP 431:排序集合。
Natural order versus insertion order
That is a contradiction in terms. Natural ordering is not insertion order.
compareTo
method required by theComparable
interface.List#reversed
So build the list in the normal direction. Afterwards, in Java 21+, simply call
List#reversed
to get a view on that list that presents an encounter order that is the opposite of the default encounter order.When run:
To learn more, see JEP 431: Sequenced Collections.