java 在java中将元素添加到iterable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27382722/
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
add element to iterable in java
提问by ovod
I have the following code:
我有以下代码:
public class Router {
private Iterable<Route> routes;
public Router(Iterable<Route> routes) {
this.routes = routes;
}
public void addRoute(Route route) {
routes.add(route);\problem is here
}
}
I highlighted the line which is not working. Here I try to add a new object to the routes. In main file routes are:
我突出显示了不起作用的行。在这里,我尝试向路线添加一个新对象。在主要文件路由是:
public class RouterMain
{
public static void main(String[] arg) throws IllegalArgumentException
{
List<Route> routes = new ArrayList<Route>();
Router router = new Router(routes);
}
}
The task is to add an object in iterable object in Router
class. As I understood Iterable
can iterate, not add something. So what should I do, convert routes in Router
class to a List
, add an element and go back?
任务是在Router
类中的可迭代对象中添加一个对象。据我了解Iterable
可以迭代,而不是添加一些东西。那么我该怎么做,将Router
class 中的路由转换为 a List
,添加一个元素并返回?
采纳答案by Mustafa J
If you want to add to it you could create a new list and add all the current elements to that list and then just add the object you would like after e.g
如果您想添加到它,您可以创建一个新列表并将所有当前元素添加到该列表中,然后只需添加您想要的对象,例如
public class Router {
private Iterable<Route> routes;
public Router(Iterable<Route> routes) {
this.routes = routes;
}
public void addRoute(Route route) {
//create new list
ArrayList<Route> myList = new ArrayList<Route>();
//iterate through current objects and add them to new list
Iterator<Route> routeIterator = routes.iterator();
while(routeIterator.hasNext()){
myList.add(routeIterator.next());
}
//add object you would like to the list
myList.add(route);
}
}
回答by Eran
Iterable<Router>
is used to enable iterating over the Router
elements contained in your class. It is not a Collection, though many collections implement this interface. It doesn't have the add
method. It only has a method that returns an Iterator<Router>
.
Iterable<Router>
用于启用对Router
类中包含的元素的迭代。它不是一个集合,尽管许多集合实现了这个接口。它没有add
方法。它只有一个返回Iterator<Router>
.
You should use some Collection (List, Set, etc...) to store your routes. Those collections have add
method.
您应该使用一些集合(列表、集合等...)来存储您的路线。那些集合有add
方法。
public class Router {
private List<Route> routes = new ArrayList<Route>();
public Router(Iterable<Route> routes) {
for (Route route : routes)
this.routes.add(route);
}
}
回答by Ernesto Campohermoso
Iterable doesn't has the method add. Maybe you need an AbstractCollection
Iterable 没有 add 方法。也许你需要一个 AbstractCollection
http://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html
http://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html
回答by Aaron Digulla
Iterable
is a type which is mostly used in loops and utility functions - you should avoid it in general. I suggest to change the constructor to Router(Collection<Route> routes)
and use the Collection
interface internally. That way, you can use almost any collection type (sets, and lists).
Iterable
是一种主要用于循环和实用函数的类型 - 通常应该避免使用它。我建议将构造函数更改为Router(Collection<Route> routes)
并在Collection
内部使用该接口。这样,您几乎可以使用任何集合类型(集合和列表)。
For additional safety, make a copy of the collection - then you can't be hurt by someone making changes to the collection outside of your code.
为了更加安全,请复制该集合 - 这样您就不会因为有人在您的代码之外对集合进行更改而受到伤害。
回答by Mikhail
Use List
interface instead of Iterable
. It has a method listIterator
. Which returnes an instance of ListIterator
. In this interface you can both add and remove items. Iterable does not look to fit in your task.
使用List
interface 而不是Iterable
. 它有一个方法listIterator
。它返回ListIterator
. 在此界面中,您可以添加和删除项目。Iterable 看起来不适合您的任务。