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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With