java 用于存储多种数据类型的列表类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1905452/
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
Type of List to Store Multipe Data Types
提问by Michael
So my problem is that I've written a function that takes two Doubles, two Int, and a Calendar object on Android (Java). I believe the class provided to allow it to run in a separate thread, AsyncTask, accepts only one type of Object (but allows multiple) as an argument so I figured I might be able to put it in a List or a LinkedList or something.
所以我的问题是我在 Android (Java) 上编写了一个函数,该函数需要两个 Double、两个 Int 和一个 Calendar 对象。我相信提供的类允许它在单独的线程中运行,AsyncTask,只接受一种类型的对象(但允许多个)作为参数,所以我想我可以把它放在 List 或 LinkedList 或其他东西中。
Is there such a type that allows multiple data types like that (Double, Double, Int, Int, Calendar), or would I have to create my own object class? I'm a novice programmer so less complicated is probably better, but I'm interested in the best solution as well.
是否有这样的类型允许像 ( Double, Double, Int, Int, Calendar)这样的多种数据类型,或者我是否必须创建自己的对象类?我是一名新手程序员,所以不太复杂可能更好,但我也对最佳解决方案感兴趣。
What the function does is take a location (double latitude, double longitude), a couple options as integers, and a Calendar Object. It takes the location, options, and date then returns a Time object of the sunrise (or sunset, depending on the options) for that location. Thanks for the tips, and I understand it would probably be best to create a special object class and just pass that, or override the background thread class, but I'm pretty new to object-oriented programming so the less overhead the better (for now).
该函数的作用是获取一个位置 ( double latitude, double longitude)、几个作为整数的选项和一个日历对象。它获取位置、选项和日期,然后返回该位置的日出(或日落,取决于选项)的 Time 对象。感谢您的提示,我知道最好创建一个特殊的对象类并直接传递它,或者覆盖后台线程类,但我对面向对象编程还很陌生,所以开销越少越好(对于现在)。
(Update) After a lot of work, it ended up being easier making a data-type class and just using that. The right way turned out to be easier in the end. Who'd a thought.
(更新)经过大量工作,最终制作一个数据类型类并使用它变得更容易。事实证明,正确的方法最终会更容易。谁想到。
回答by BalusC
Just
只是
List<Object> objects = new ArrayList<Object>();
or so?
或者?
I wouldn't recommend this approach though. The data must be somehow related to each other. Why would you make it hard for yourself and mix different types of data in a collection? What do you need it for at end? Just passing it around through layers? You could also just create a custom javabean object for this (also known as value object or data transfer object). Something like:
不过我不推荐这种方法。数据必须以某种方式相互关联。为什么要让自己变得困难并在集合中混合不同类型的数据?你最后需要它做什么?只是通过层传递它?您也可以为此创建一个自定义的 javabean 对象(也称为值对象或数据传输对象)。就像是:
public class Data {
private Double double1;
private Double double2;
private int int1;
private int int2;
private Calendar calendar;
// Add/generate getters and setters.
}
回答by Myles
The least complicated method would be to have the list be of type Objectand store the items in a List that way.
最简单的方法是让列表具有类型Object并以这种方式将项目存储在列表中。
回答by Rich Schuler
So you're really looking for the answer to the wrong question. The problem you're having isn't the one you've described but the fact that you're needing to ask it. In other words, if it feels like you're doing something wrong or trying to fit a square peg into a round hole it's because you probably are. If you have complete control over the code look for alternative methods of implementing to gain your desired results. I'm 100% sure that the naive solution, a list that holds Objectsis bad. :) Good luck.
所以你真的在寻找错误问题的答案。您遇到的问题不是您所描述的问题,而是您需要提出问题的事实。换句话说,如果感觉自己做错了什么或试图将方钉插入圆孔中,那是因为你可能是。如果您可以完全控制代码,请寻找替代的实现方法以获得所需的结果。我 100% 确定天真的解决方案,一个包含的列表Objects是不好的。:) 祝你好运。

