2

I need to create an ArrayList and fill it with BigDecimals and a Date elements, how do I do?

Should I create a new Class with all the types I need in the ArrayList and then use it as the type?

8
  • 10
    you can, doesn't mean you should ;-) Commented Nov 8, 2013 at 9:03
  • 3
    It is possible, but from a design-perspective you should really not do that. There is a reason for the strong typing of Java and you can for sure solve your problem without trying to break it Commented Nov 8, 2013 at 9:06
  • @LionC : what you are saying is absolutely correct ,please mention the reason in short Commented Nov 8, 2013 at 9:10
  • You can, but Java is strongly typed language. This should make you think that you're idea it's not so brilliant. Commented Nov 8, 2013 at 9:10
  • 2
    You should create a class which has a field for each piece of information you want to store. An arraylist might be simple to create but working with A collection of data will be a nightmare compared with using a proper class. Commented Nov 8, 2013 at 9:14

5 Answers 5

6

It would be interesting to know what you try to achieve.

If the BigDecimals and Dates do have a logical relation, e.g. like a the amount of a bank transaction and it's placed date, than you should think about introducing a new class that brings both together (hopefully named BankTransaction). You can than put objects of this class in the List.

If the BigDecimals and Dates have nothing to do with each other why do you want to store them in the same list? In this case you will confuse other developers since they must take a look at the code that interpretes the List and they can not guess what it means because of the list's type.

Nevertheless you can use the List<Object> approach, but this would not be self-explanantory code like List<BankTransaction> for example.

Sign up to request clarification or add additional context in comments.

3 Comments

If I interpret OP:s question correctly, this answer should help the most.
Yes the date has a relation with the other components, and it's about a BankTransaction so I will create a new class. Thanks
Another case of Object Denial solved ;-)
1

Use code like below:

List<Object> list2 = new ArrayList<Object>();
list2.add(new BigDecimal(3242));
list2.add(new Date());
for (Object object : list2) {
  if(object instanceof Date) {
    // your logic on date
  } else if (object instanceof BigDecimal) {
    // your logic on BigDecimal
  }
}

2 Comments

Sorry, I have to downvote. While this is technically correct, I can't condone such an approach without at least a comment on why it's a bad idea.
@Joachim Sauer,Agreed. But about down vote, the question got up votes, why an answer to such a question down vote?
1

Just use a List<Object>

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

Although this way works properly and fits your question, you should definitly check your requirements and if you really need to add Dates and BigDecimals to the same List, as this is not a good practice.

4 Comments

But it's really dirty, consider making 2 Lists instead ;)
Java 7 is not brand-new now, I think we can safely use new ArrayList<>() in our example code now.
@Theolodis thats true, but without more information about the exact requirement it is hard to recommend something that would fit better.
@user1983983 this wasn't for you, but for the OP ;) Maybe you add it to the answer, just to make sure noone uses it without considering any other (better) solution :P
0

All classes in Java inherit from the base Object class, so you can just create an ArrayList that accepts Object instances, and put whatever you like in it:

List<Object> list = new ArrayList<Object>();
list.add(new Date()); // adds a Date instance
list.add(new BigDecimal()); // adds a BigDecimal

Comments

0
 ArrayList<Object> list = new ArrayList<Object>()

try this. Use an Object class of java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.