Arief Rivai
Arief Rivai

Reputation: 228

How to add element to list Double programatically in java

I have syntax:

List<double[]> x = new ArrayList<double[]>();
x.add(new double[] { 5,6,7,8 });

How to add 5,6,7,8 automatically? like this

for (int i=5; i<=8; i++) {
**CODE**
}

List<double[]> x = new ArrayList<double[]>();
x.add(new double[] { **CODE** });

So, I want to replace **CODE**, what is that **CODE**? is it possible? Sorry bad English

Upvotes: 0

Views: 2668

Answers (2)

Steve Benett
Steve Benett

Reputation: 12933

double[] d = new double[4];
for (int i=5; i<=8; i++) {
    d[i-5] = i; 
}

List<double[]> x = new ArrayList<double[]>();
x.add(d);

Didnt test but should work if u wanna add a Array to an ArrayList.

Upvotes: 3

A human being
A human being

Reputation: 1220

Replace double with Double in your code. Like this:

List<Double> x = new ArrayList<Double>();
for (int i=5; i<=8; i++) {
   x.add(new Double((double)i));
}

Upvotes: 0

Related Questions