清单类型来存储多发性数据类型数据类型、清单、多发性、类型

2023-09-06 15:48:59 作者:給我閉嘴

所以,我的问题是,我写了一个函数,它有两个双打,两个int,并在Android(Java)的日历对象。我相信提供的类,使其能够在一个单独的线程中运行,AsyncTask的,只接受一种类型的对象(但允许多个)作为参数,所以我想我也许可以把它放在一个列表或一个LinkedList的什么的。

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.

有没有这样一种类型,它允许这样的多种数据类型(双,双,INT,INT,日历),或将我要创建我自己的对象类?我是一个新手程序员,所以不太复杂的可能会更好,但我感兴趣的是最好的解决方案也是如此。

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.

函数的作用是采取一个位置(双纬度,经度双),一对夫妇选择为整数,和日历对象。它需要的位置,选项和日期再返回日出的时间对象(或日落,根据选项),该位置。感谢您的提示,我知道它很可能是最好创建一个特殊的对象类,只是传递,或覆盖后台线程类,但我pretty的新的面向对象编程,以便少开销更好(现在)。

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).

(更新)大量的工作后,结束了更容易使一个数据类型类和公正使用。正确的方法竟然是到底更容易。谁愿意一个念头。

(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.

推荐答案

只是

List<Object> objects = new ArrayList<Object>();

左右?

我不推荐这种方法虽然。数据必须以某种方式彼此相关。为什么你会使其难以为自己和一个集合混合不同类型的数据?你怎么在年底需要它?只是通过它身边经过层层?你也可以只创建此自定义JavaBean对象(也称为值对象或数据传输对象)。是这样的:

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:

public class Data {
    private Double double1;
    private Double double2;
    private int int1;
    private int int2;
    private Calendar calendar;
    // Add/generate getters and setters.
}