java 以相反的顺序打印任何集合中的项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/196628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:22:53  来源:igfitidea点击:

Printing out items in any Collection in reverse order?

javacollections

提问by 101010110101

I have the following problem in my Data Structures and Problem Solving using Javabook:

我的数据结构和问题解决使用 Java书中有以下问题:

Write a routine that uses the Collections API to print out the items in any Collection in reverse order. Do not use a ListIterator.

编写一个例程,使用集合 API 以相反的顺序打印出任何集合中的项目。不要使用 ListIterator。

I'm not putting it up here because I want somebody to do my homework, I just can't seem to understand exactly what it is asking for me to code!

我把它放在这里不是因为我想让别人做我的作业,我似乎无法完全理解它要求我编写代码的内容!

When it asks me to write a 'routine', is it looking for a single method? I don't really understand how I can make a single method work for all of the various types of Collections (linked list, queue, stack).

当它要求我编写“例程”时,它是否在寻找一种方法?我真的不明白如何使单一方法适用于所有各种类型的集合(链表、队列、堆栈)。

If anybody could guide me in the right direction, I would greatly appreciate it.

如果有人能指导我朝着正确的方向前进,我将不胜感激。

回答by ddimitrov

Regardless from the question not making much sense as half of the collections have no gstable ordering of have fixed-ordering (i.e. TreeSet or PriorityQueue), you can use the following statement for printing the contents of a collection in reverse-natural order:

不管这个问题没有多大意义,因为一半的集合没有 gstable 排序或固定排序(即 TreeSet 或 PriorityQueue),您可以使用以下语句以逆自然顺序打印集合的内容:

List temp = new ArrayList(src);
Collections.reverse(temp);
System.out.println(temp);

I essence you create an array list as lists are the only structure that can be arbitrarily reordered. You pass the srccollection to the constructor which initializes the list withj the contents of the srcin the collection natural order. Then you pass the list to the Collections.reverse()method which reverses the list and finally you print it.

我本质上创建了一个数组列表,因为列表是唯一可以任意重新排序的结构。您将src集合传递给构造函数,该构造函数以集合自然顺序使用src的内容初始化列表。然后将列表传递给Collections.reverse()方法,该方法反转列表,最后打印它。

回答by AdamC

First, I believe it is asking you to write a method. Like:

首先,我相信它是要求您编写一个方法。喜欢:

void printReverseList(Collection col) {}

Then there are many ways to do this. For example, only using the Collection API, use the toArray method and use a for loop to print out all the items from the end. Make sense?

那么有很多方法可以做到这一点。例如,仅使用 Collection API,使用 toArray 方法并使用 for 循环从末尾打印出所有项。说得通?

As for the various classes using the Collection interface, it will automatically work for all of those since they must implement the interface (provided they implement it in a sane way;).

至于使用 Collection 接口的各种类,它将自动适用于所有这些类,因为它们必须实现该接口(前提是它们以一种理智的方式实现它;)。

回答by Moishe Lettvin

Isn't there a base Collection class?

没有基本的 Collection 类吗?

Probably worth looking here as a starting point...

可能值得将这里作为起点...

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html

回答by Kris

I don't know much Java, but considering the "Collections API" i imagine all those objects implement an interface you could iterate through someway. i suppose they all could have an itemAtIndex( int index ) and length() or similar method you could use.

我不太了解 Java,但考虑到“Collections API”,我想所有这些对象都实现了一个可以以某种方式迭代的接口。我想他们都可以有一个 itemAtIndex( int index ) 和 length() 或者你可以使用的类似方法。

You might want to read this.

您可能想阅读此内容。

回答by Karan

Well you could have a routine that delegates to other routines based on the input type, however I'm not sure there is a generic enough collection type that can be encompassed into one argument. I guess you could just use method overloading (having multiple methods with the same name, but accept different args).

好吧,您可以拥有一个基于输入类型委托给其他例程的例程,但是我不确定是否有足够通用的集合类型可以包含在一个参数中。我想你可以只使用方法重载(有多个具有相同名称的方法,但接受不同的参数)。

That could technically count as 1 routine (all have the same name).

从技术上讲,这可以算作 1 个例程(都具有相同的名称)。