开发者

Type of List to Store Multipe Data Types

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.

Is there such a type that allows multiple data types like that (Double, Double, Int, Int, Calendar), or would I have开发者_如何学Go 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.

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.


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:

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


The least complicated method would be to have the list be of type Object and store the items in a List that way.


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 Objects is bad. :) Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜