0

I want to make an ArrayList that can hold both a double and an int.

i.e.:

List<[_classNameHere_]> doublesAndInt = new ArrayList<_classNameHere_>();
ernest_k
  • 44,416
  • 5
  • 53
  • 99
the awe
  • 1
  • 1
  • 8
    [`Number`](https://docs.oracle.com/javase/8/docs/api/java/lang/Number.html) should work – MasterBlaster Jul 17 '18 at 18:10
  • I am new and don't know how this site works but that helps :) ty – the awe Jul 17 '18 at 18:15
  • Use any of the common superclasses: Number, Comparable (with appropriate generics), Serializable, Object. – Andy Turner Jul 17 '18 at 18:15
  • 1
    Your question is ambiguous. To clarify: do you want a list, where some elements of the list are `double` and some are `int`? Or do you want a list of pairs of `double` and `int`? Do you need `double` and `int` specifically, or just a way to store data that could be integers or could be floating-point? In the latter case, a `List` (or even just `double[]`) may suffice, because `double` can store any integer up to `2**53` exactly, which is greater than the range of `int` in Java (but less than the range of `long`). – Daniel Pryden Jul 17 '18 at 18:24
  • When using @DanielPryden solution you could then use the answer from [this question](https://stackoverflow.com/questions/15963895/how-to-check-if-a-double-value-has-no-decimal-part) to extract the `double`s back into an `int` (if it really is one) – Lino Jul 17 '18 at 18:26

0 Answers0