Java 如何将一个对象添加到另一个对象集中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3545183/
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
How to add an object into another Object set
提问by
I have two classes. One(Person) for getters and setters, and another(People) for compute the data. What is my situation is, I get the data from the DB using ResultSet
, then created a person Object to store the row data. Then i created people Object to store all persons.
我有两节课。一个(Person) 用于getter 和setter,另一个(People) 用于计算数据。我的情况是,我使用从数据库获取数据ResultSet
,然后创建了一个人对象来存储行数据。然后我创建了 people 对象来存储所有人。
Each Object created as SET.
每个对象创建为 SET。
while(rs.next())
{
Set<People> people = new HashSet<people>();
Person person = new Person();
String name = rs.getString(2);
person.setName(name);
int id = rs.getInt(1);
person.setId(id);
String dept = rs.getString(4);
person.setDept(dept);
int age = rs.getInt(3);
person.setAge(age);
people.add(person);
}
return people;
Now the problem is the last line in the While Loop people.add(person);
现在的问题是 While 循环中的最后一行 people.add(person);
It says
它说
The method add(People) in the type Set is not applicable for the arguments (Person)
类型 Set 中的 add(People) 方法不适用于参数 (Person)
How can i overcome this problem?
我怎样才能克服这个问题?
Thanks.
谢谢。
采纳答案by Andreas Dolk
My understandig from your design is that you have a People has-many Personrelation, so the People
class holds a collection of Person
objects. Then I'd expect something like this:
我对您的设计的理解是,您有一个People 与多个 Person关系,因此People
该类包含一个Person
对象集合。然后我会期待这样的事情:
public class Person {
private String name;
private Date dateOfBirth;
// .. more attributes
// getters and setters
// overrides of equals, hashcode and toString
}
public class People implements Set<Person> {
private Set<Person> persons = new HashSet<Person>();
public boolean add(Person person) {
return persons.add(person);
}
// more methods for remove, contains, ...
}
So in your database related code you wouldn't need to create another set, because People
already has the one you need:
因此,在与数据库相关的代码中,您不需要创建另一个集合,因为People
已经有了您需要的集合:
People people = new People(); // or get it, if it's already created
while(rs.next())
{
Person person = new Person();
String name = rs.getString(2);
person.setName(name);
int id = rs.getInt(1);
person.setId(id);
String dept = rs.getString(4);
person.setDept(dept);
int age = rs.getInt(3);
person.setAge(age);
people.add(person);
}
return people;
回答by Gopi
Based on what you are trying to do I feel, you should convert your Person to People class before adding to the set. Your People class may have a constructor which takes a Person as an argument and copies the required fields from Person to People. Here your code to add to set will look like people.add(new People(person));
根据我的感觉,您应该将 Person 类转换为 People 类,然后再添加到集合中。你的 People 类可能有一个构造函数,它接受一个 Person 作为参数,并将所需的字段从 Person 复制到 People。在这里,您要添加到 set 的代码将如下所示people.add(new People(person));
When you declare Set<People> people = new HashSet<People>();
what it means is that this set is supposed to contain objects of 'type' People i.e. instances of People or instances of subclasses of People. If People is an interface, then the set may contain any object that implements the interface.
当您声明时Set<People> people = new HashSet<People>();
,这意味着该集合应该包含“类型” People 的对象,即 People 的实例或 People 的子类的实例。如果 People 是一个接口,则该集合可能包含实现该接口的任何对象。
回答by drstupid
I don't understand why you would want 2 classes in the first place. You cand have Person implement the computational part as well. But, nevertheless, what you could do:
我不明白为什么你首先想要 2 个班级。您也可以让 Person 实现计算部分。但是,尽管如此,您可以做的是:
class People implements Set<Person> {
private HashSet<Person> hashset = new HashSet<Person>();
// ... your computational code goes here
// delegate all Set methods to hashset
}
and then:
进而:
People people = new People();
while(rs.next())
{
Person person = new Person();
String name = rs.getString(2);
person.setName(name);
int id = rs.getInt(1);
person.setId(id);
String dept = rs.getString(4);
person.setDept(dept);
int age = rs.getInt(3);
person.setAge(age);
people.add(person);
}
return people;
回答by u290629
I don't think that Set<People> people = new HashSet<people>();
should be written in the loop.
我认为不Set<People> people = new HashSet<people>();
应该写在循环中。
回答by double-m
I understand Person
is a data structure (bean-like, with getters and setters), and People
should contain all Person
objects from the database and perform calculations on them.
我理解Person
是一种数据结构(类似bean,带有getter 和setter),People
应该包含Person
数据库中的所有对象并对其执行计算。
If that is true, first of all, you cannot declare people within the loop (because a new People
object will be created for each Person
, and you don't want that, from what I understand).
如果这是真的,首先,您不能在循环中声明 people (因为People
将为每个 创建一个新对象Person
,而根据我的理解,您不希望那样)。
Second, People
needs to be able to contain Person objects. So it should at least be composed of a Set
of Person
objects. You can add more functionality as you please. So, try something like this:
其次,People
需要能够包含 Person 对象。因此,它至少应该组成的Set
的Person
对象。您可以根据需要添加更多功能。所以,尝试这样的事情:
public class People {
Set<Person> persons = new HashSet<Person>();
Set<Person> getPersons() {
return persons;
}
int computeSomethingAboutPeople() {
// return as you please
}
}
And use it like this, as the previous poster suggested:
并像这样使用它,正如之前的海报所建议的那样:
People people = new People();
while(rs.next())
{
Person person = new Person();
String name = rs.getString(2);
person.setName(name);
int id = rs.getInt(1);
person.setId(id);
String dept = rs.getString(4);
person.setDept(dept);
int age = rs.getInt(3);
person.setAge(age);
people.getPersons().add(person);
}
int answer = people.computeSomethingAboutPeople();
回答by Shivam Thacker
class Cartesian
{
double x,y,z;
public
Cartesian()
{
x=y=z=0;
}
Cartesian (int i,int j,int k)
{
x=i;
y=j;
z=k;
}
Cartesian add_coordinates(Cartesian c)
{
c.x=x+c.x;
c.y=y+c.y;
c.z=z+c.z;
return c;
}
void display()
{
System.out.println("Addition of coordinates is : "+x+"i "+y+"j "+z+"k ");
}
}
class Coordinate
{
public static void main(String[] args)
{
Cartesian obj1 = new Cartesian(5,5,-10);
Cartesian obj2 = new Cartesian(5,5,-10);
Cartesian obj3 = new Cartesian();
obj3=obj1.add_coordinates(obj2);
obj3.display();
}
}