Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type of List to Store Multipe Data Types

Tags:

java

android

list

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.

like image 717
Michael Avatar asked Dec 03 '22 13:12

Michael


2 Answers

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.
}
like image 154
BalusC Avatar answered Dec 26 '22 05:12

BalusC


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

like image 32
Myles Avatar answered Dec 26 '22 03:12

Myles