简体   繁体   English

如何在Java中将对象添加到集合中?

[英]How can I add an object into a set in Java?

I am writing some JUnit tests for a bus stop but I am having trouble trying to create objects to be used through the tests: 我正在为公交车站编写一些JUnit测试,但在尝试创建要通过测试使用的对象时遇到了麻烦:

public class StopTests {
  BusRoute rte = new BusRoute("250");
  Set<BusRoute> set = new Set<BusRoute>();
  BusStop stop = new BusStop(00000, "Staples Center", 90.0, 90.0, set);
...
}

My problem is that the test won't compile because Eclipse says "new Set()" cannot be instantiated. 我的问题是测试无法编译,因为Eclipse表示无法实例化“ new Set()”。 My intention is to add 'rte' into 'set' so that 'stop' can be created without compilation errors, but I am stumped on how to do this. 我的意图是将“ rte”添加到“ set”中,以便可以在没有编译错误的情况下创建“ stop”,但是我对如何执行此操作感到困惑。 I tried approaching it like: 我试图接近它:

public class StopTests {
  BusRoute rte = new BusRoute("250");
  Set<BusRoute> set = new Set<BusRoute>();
  set.add(BusRoute rte);
  BusStop stop = new BusStop(00000, "Staples Center", 90.0, 90.0, set);
...
}

but Eclipse gave me another error about not having an identifier following 'add'. 但是Eclipse给了我另一个错误,关于“添加”之后没有标识符。

What would be the best way to approach this? 解决此问题的最佳方法是什么?

EDIT: This is what I have now: 编辑:这就是我现在所拥有的:

public class StopTests {
  BusRoute rte = new BusRoute("250");
  Set<BusRoute> set = new HashSet<BusRoute>();
  set.add(rte);
  BusStop stop = new BusStop(00000, "Staples Center", 90.0, 90.0, set);
...
}

Set is an interface so cannot create an instance of it. Set是一个接口,因此无法创建它的实例。 You need to create and instance of Set implementations such as HashSet . 您需要创建Set实现的实例,例如HashSet

Try changing this: 尝试更改此:

Set<BusRoute> set = new Set<BusRoute>()

to

Set<BusRoute> set = new HashSet<BusRoute>()

Also to add the element in set you need to call add on set instance and not on BusRoute instance. 另外,要在集合中添加元素,您需要在集合实例而不是BusRoute实例上调用add So change this : 所以改变这个:

  rts.add(BusRoute rte);

to

  set.add(rte);

Your issue here is with this line: rts.add(BusRoute rte) . 您的问题是: rts.add(BusRoute rte) You don't need to prepend rte with its type: use rts.add(rte) instead. 你并不需要预先rte和它的类型:使用rts.add(rte)来代替。

Set is an interface, you need to instantiate with an implementation class: Set是一个接口,您需要使用一个实现类实例化:

Set<BusRoute> set = new HashSet<BusRoute>()

Please see these links 请查看这些链接

Set examples 设置例子

Set API 设定API

Cheers !! 干杯!

Set is abstract so it cannot be instantiated. Set是抽象的,因此无法实例化。 You need to use an implementation of Set , such as HashSet . 您需要使用Set的实现,例如HashSet

public class StopTests {

    public void someMethod() {
        BusRoute rte = new BusRoute("250");
        Set<BusRoute> set = new HashSet<BusRoute>();
        set.add(rte);
        BusStop stop = new BusStop(00000, "Staples Center", 90.0, 90.0, set);
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java - 如何将 object 添加到另一个 object 的列表中 - Java - How can I add an object to a list of another object 如何在Java中为可移动对象增加速度? - How can I add velocity to a movable object in Java? JAVA如何读取SQL并添加对象数组 - JAVA how can I readl a SQL and add in a object array 如何使用Java和SWT将ActiveX对象添加到ROT? - How can I add ActiveX object to ROT using Java and SWT? Java:如何通过用户输入将对象添加到列表中? - Java: How can I add an object in a List with user input? 如何在Java中将方法添加到现有对象? - How can I add method to an existing object in Java? 如何在反应式Java中将新对象添加到现有流中? - How can I add in reactive Java a new Object to an existing stream? 如何使用Java流将一组对象转换为该对象的字段映射到一组字段? - How can I convert a Set of objects into a Map of the Object's fields to a set of fields using Java streams? 如何使用jUnit testing(java)测试— void add(Object val)和Object at(int index)? - How can I test for — void add(Object val) and Object at(int index) using jUnit testing(java)? How can i add a json object from an Api call to a Java Class object? - How can i add a json object from an Api call to a Java Class object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM