18

Is there a way to define an ArrayList with the double type? I tried both

ArrayList list = new ArrayList<Double>(1.38, 2.56, 4.3);

and

ArrayList list = new ArrayList<double>(1.38, 2.56, 4.3);

The first code showed that the constructor ArrayList<Double>(double, double, double) is undefined and the second code shows that dimensions are required after double.

dana
  • 17,267
  • 6
  • 64
  • 88
Robert Lu
  • 441
  • 2
  • 7
  • 18

8 Answers8

30

Try this:

List<Double> list = Arrays.asList(1.38, 2.56, 4.3);

which returns a fixed size list.

If you need an expandable list, pass this result to the ArrayList constructor:

List<Double> list = new ArrayList<>(Arrays.asList(1.38, 2.56, 4.3));
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thanks. I knew I was missing something. Everyone's answers helped but changing the type to List did the trick. – Robert Lu Jul 05 '13 at 05:09
  • 2
    [`Arrays.asList`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#asList(T...)) does not return an unmodifiable list. It returns a fixed-size list. You can `set` elements, but you cannot `add` or `remove`. – Jeffrey Jul 05 '13 at 05:19
  • @Jeffrey Thanks for posting! I never realised that... I had always assumed it was unmodifiable, because it exploded when I tried to add. I never tried *modifying* via set. Answer updated. Cheers. – Bohemian Jul 05 '13 at 05:23
  • I don't get it ... Can I get some kind of explanation as to why the answer by @Rohan Salunkhe and voidHead is not the best one to use in this case? – Luke Burgess Feb 06 '16 at 21:13
  • @luke maybe because rohan's answer is an exact copy of part of my answer (and posted after mine), but without any explanation, voidHead's overs (IMHO inelegant) multiline solutions, whereas both approaches in my answer are single lines. – Bohemian Feb 07 '16 at 01:46
  • OK I see yours was first to be posted; but this is what works for me ... ArrayList numb= new ArrayList([1.38, 2.56, 4.3]); your method throws warnings on my compiler. – Luke Burgess Feb 07 '16 at 03:05
  • @luke I just corrected a typo (missing diamond in 2nd version), which would have generated a warning. Both versions now compile. Note that unless you absolutely require the `ArrayList` implementation of `List`, you should declare your variable as `List` not `ArrayList` - see [Liskov substitution principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle). Note also that `Arrays.asList()` returns a `List` (not an `ArrayList`) - another reason to use a `List`. Your code doesn't compile in java; I suspect you're using groovy, in which `[1.38, 2.56, 4.3]` is a `List`. – Bohemian Feb 07 '16 at 04:43
  • Thank you, that explanation means a lot to me. I am using the OpenJDK javac on Ubuntu. – Luke Burgess Feb 07 '16 at 17:50
4

Try this,

ArrayList<Double> numb= new ArrayList<Double>(Arrays.asList(1.38, 2.56, 4.3));
Rohan
  • 3,068
  • 1
  • 20
  • 26
3

You are encountering a problem because you cannot construct the ArrayList and populate it at the same time. You either need to create it and then manually populate it as such:

ArrayList list = new ArrayList<Double>();
list.add(1.38);
...

Or, alternatively if it is more convenient for you, you can populate the ArrayList from a primitive array containing your values. For example:

Double[] array = {1.38, 2.56, 4.3};
ArrayList<Double> list = new ArrayList<Double>(Arrays.asList(array));
x4nd3r
  • 855
  • 1
  • 7
  • 20
2

You can use Arrays.asList to get some list (not necessarily ArrayList) and then use addAll() to add it to an ArrayList:

new ArrayList<Double>().addAll(Arrays.asList(1.38L, 2.56L, 4.3L));

If you're using Java6 (or higher) you can also use the ArrayList constructor that takes another list:

new ArrayList<Double>(Arrays.asList(1.38L, 2.56L, 4.3L));
Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
Itay Maman
  • 30,277
  • 10
  • 88
  • 118
2

Using guava

Doubles.asList(1.2, 5.6, 10.1);

or immutable list

ImmutableList.of(1.2, 5.6, 10.1);
Sergii Shevchyk
  • 38,716
  • 12
  • 50
  • 61
2

1) "Unnecessarily complicated" is IMHO to create first an unmodifiable List before adding its elements to the ArrayList.

2) The solution matches exact the question: "Is there a way to define an ArrayList with the double type?"

double type:

double[] arr = new double[] {1.38, 2.56, 4.3};

ArrayList:

ArrayList<Double> list = DoubleStream.of( arr ).boxed().collect(
    Collectors.toCollection( new Supplier<ArrayList<Double>>() {
      public ArrayList<Double> get() {
        return( new ArrayList<Double>() );
      }
    } ) );

…and this creates the same compact and fast compilation as its Java 1.8 short-form:

ArrayList<Double> list = DoubleStream.of( arr ).boxed().collect(
    Collectors.toCollection( ArrayList::new ) );
Kaplan
  • 21
  • 1
1

Try this:

 List<Double> l1= new ArrayList<Double>();
 l1.add(1.38);
 l1.add(2.56);
 l1.add(4.3);
Yuriy Zaletskyy
  • 4,983
  • 5
  • 34
  • 54
Manish Doshi
  • 1,205
  • 1
  • 9
  • 17
-1
double[] arr = new double[] {1.38, 2.56, 4.3};

ArrayList<Double> list = DoubleStream.of( arr ).boxed().collect(
    Collectors.toCollection( new Supplier<ArrayList<Double>>() {
      public ArrayList<Double> get() {
        return( new ArrayList<Double>() );
      }
    } ) );
Kaplan
  • 1