Type of List to Store Multipe Data Types

前端 未结 3 1609
情话喂你
情话喂你 2021-01-13 21:22

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

相关标签:
3条回答
  • 2021-01-13 21:47

    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.
    }
    
    0 讨论(0)
  • 2021-01-13 21:47

    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 讨论(0)
  • 2021-01-13 21:51

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

    0 讨论(0)
提交回复
热议问题