List type for storing multiple data types

So my problem is that I wrote a function that takes two doubles, two Int objects, and a Calendar object on Android (Java). I believe that the class provided to run in a separate thread, AsyncTask, accepts only one type of object (but allows several) as an argument, so I decided that I could put it in a list or LinkedList or something like that.

Is there a type that allows you to use several data types ( Double, Double, Int, Int, Calendar ), or do I need to create my own class of objects? I am a newbie programmer, so it’s less difficult, probably better, but I am also interested in a better solution.

What the function does is take a location ( double latitude, double longitude ), a couple of options in the form of integers and a calendar object. It takes the location, parameters, and date, and then returns the sunrise time object (or sunset, depending on the parameters) for that location. Thanks for the tips, and I understand that it would be better to create a special class of objects and just pass this or override the background thread class, but I'm pretty new to object-oriented programming, so the less overhead, the better (for currently )

(Update). After much work, it became easier to create a data type class and just use it. The correct path turned out to be easier in the end. Who thought.

+4
source share
3 answers

Just

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

or so?

I would not recommend this approach. 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 for this? Just pass it through the layers? You can also create your own javabean object for it (also known as a value object or data transfer object). Sort of:

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

The least complicated method is for the list to be of type Object and thus save the elements in the list.

+1
source

So, you are really looking for the answer to the wrong question. The problem you are facing is not the one you described, but the fact that you need to ask him. In other words, if it seems to you that you are doing something wrong or trying to put a square anchor in a round hole, this is probably the case. If you have full control over the code, find alternative implementation methods to get your desired results. I am 100% sure that a naive solution, a list containing Objects , is bad. :) Good luck.

0
source

All Articles