java 如何在容器中存储不同类型的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12835500/
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 store objects of different types in a container?
提问by Biscuitz
I'm wondering if i can have an array ( or basically a table ) with each of its element being a set of objects of different types. I mean i want to create something like this ( i know it's incorrect in syntax, just wanna demonstrate my idea ) :
我想知道我是否可以拥有一个数组(或基本上是一个表),其中每个元素都是一组不同类型的对象。我的意思是我想创建这样的东西(我知道它的语法不正确,只是想展示我的想法):
List<String, int, double, Date, ... , etc > list_name
I'm doing this to ensure that when I save all these information to my database, i will have all these information in the same entry in the database. This is because i did some web scraping from different sites to gather all these data, i.e. in the list, string may be from site A, int may be from site B, etc. I found some information may be missed for some reasons ( say, for a particular element of the list, String from site A may be missing, other data are just there, perfectly fine. ). If i store these data into seperated lists, i'm afraid there will be some mismatch of data.
我这样做是为了确保当我将所有这些信息保存到我的数据库时,我会将所有这些信息放在数据库的同一个条目中。这是因为我从不同的站点进行了一些网络抓取以收集所有这些数据,即在列表中,字符串可能来自站点 A,int 可能来自站点 B 等。我发现由于某些原因可能会遗漏一些信息(比如,对于列表的特定元素,来自站点 A 的字符串可能会丢失,其他数据就在那里,完全没问题。)。如果我将这些数据存储到单独的列表中,恐怕会有一些数据不匹配。
Now my solution is to create a class, say ClassA :
现在我的解决方案是创建一个类,比如 ClassA :
ClassA{
public String info1
public int info2
public double info3
..
..
public wtever info
}
and then i will have a of list of ClassA
然后我将有一个 ClassA 列表
I'm wondering if there is a better way to achieve this?
我想知道是否有更好的方法来实现这一目标?
采纳答案by Duncan Jones
I'm doing this to ensure that when I save all these information to my database, i will have all these information in the same entry in the database.
我这样做是为了确保当我将所有这些信息保存到我的数据库时,我会将所有这些信息放在数据库的同一个条目中。
Yes, you've done the correct thing by creating a class to hold all the values.
是的,通过创建一个类来保存所有值,您已经做了正确的事情。
And, as you've already noted, you can then create a List
of this objects if you wish to perform multiple database insertions/updates.
而且,正如您已经注意到的,List
如果您希望执行多个数据库插入/更新,您可以创建一个this 对象。
Many persistence schemes operate on the basis of defining classes (known as DAOs) to represent the data stored in individual tables. Using a persistence provider that supports annotating classes (such as Hibernate) can really simplify your interaction with a database. I'd recommend you research this topicin more detail.
许多持久性方案基于定义类(称为DAO)来表示存储在单个表中的数据。使用支持注释类(例如Hibernate)的持久性提供程序可以真正简化您与数据库的交互。我建议您更详细地研究这个主题。
回答by sakthisundar
You can use simple List<java.lang.Object>
since all the classes in Java are sub-classes of java.lang.Object
. However this is error prone and can throw ClassCastException in runtime if you aren't handling it with care. Like you have mentioned List<ClassA>
would be a right one. But make sure everything is right with ClassA
when you design it.
您可以使用 simpleList<java.lang.Object>
因为 Java 中的所有类都是java.lang.Object
. 然而,这很容易出错,如果您不小心处理它,可能会在运行时抛出 ClassCastException。就像你提到的List<ClassA>
那样是正确的。但是ClassA
在设计时请确保一切正常。
回答by Juvanis
Your way of creating a class for holding these different type of properties seems good.
Then you can use an ArrayList
, an implementation of List
interface like this:
您创建用于保存这些不同类型属性的类的方法似乎很好。然后你可以使用一个ArrayList
,一个这样的List
接口实现:
ArrayList<ClassA> list = new ArrayList<ClassA>();
And then you can populate and manipulate it easily.
然后您可以轻松地填充和操作它。
For your database operations, I suggest you to use DAO(data access object)classes and mapperclasses to facilitate your persistence operations with your data source.
对于您的数据库操作,我建议您使用DAO(数据访问对象)类和映射器类来方便您对数据源的持久性操作。
回答by acerisara
I would do the same, i.e. creating an object representing the set of things you want to save (maybe there's a domain concept hidden behind?) and then have a list of that type.
我会做同样的事情,即创建一个对象,表示您要保存的一组事物(也许背后隐藏着一个域概念?),然后有一个该类型的列表。
回答by OmniOwl
What you could do, is make an Abstract Super Class which all of your classes will extend, and then make a list of that. Any class that extends this super class will then fit into the same list.
您可以做的是创建一个抽象超类,您的所有类都将扩展该类,然后列出该类。任何扩展这个超类的类都将适合同一个列表。
Just make sure you find out what type of Object you are dealing with, when you manipulate your collection.
当你操作你的集合时,确保你找出你正在处理的对象类型。
public abstract class ClassA {
// Empty
}
public class ClassB extends ClassA {
// something something
}
public class ClassC extends ClassA {
}
public class main {
List<ClassA> list.....
}
回答by EvenLisle
Since all(a truth with moderations) objects in Java inherit from Object
, you can write List<Object>...
and just cast the objects in it whenever you need them. Bear in mind that this requires you to have absolute control over what class the different objects are.
由于 Java 中的所有(有节制的事实)对象都继承自Object
,因此您可以List<Object>...
在需要时编写并在其中投射对象。请记住,这要求您对不同对象的类别有绝对的控制权。