Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Multiple Values in ArrayList at a single index

I have a double ArrayList in java like this.

List<double[]> values = new ArrayList<double[]>(2);

Now what I want to do is to add 5 values in zero index of list and 5 values in index one through looping.

The zeroth index would have values {100,100,100,100,100} The index 1 would have values {50,35,25,45,65}

and all of these values are stored in a double array in following order

double[] values = {100,50,100,35,100,25,100,45,100,65}

How can i do it?

like image 775
Fahad Abid Janjua Avatar asked Sep 21 '12 04:09

Fahad Abid Janjua


Video Answer


1 Answers

@Ahamed has a point, but if you're insisting on using lists so you can have three arraylist like this:

ArrayList<Integer> first = new ArrayList<Integer>(Arrays.AsList(100,100,100,100,100));
ArrayList<Integer> second = new ArrayList<Integer>(Arrays.AsList(50,35,25,45,65));
ArrayList<Integer> third = new ArrayList<Integer>();

for(int i = 0; i < first.size(); i++) {
      third.add(first.get(i));
      third.add(second.get(i));
}

Edit: If you have those values on your list that below:

List<double[]> values = new ArrayList<double[]>(2);

what you want to do is combine them, right? You can try something like this: (I assume that both array are same sized, otherwise you need to use two for statement)

ArrayList<Double> yourArray = new ArrayList<Double>();
for(int i = 0; i < values.get(0).length; i++) {
    yourArray.add(values.get(0)[i]);
    yourArray.add(values.get(1)[i]);
}
like image 179
yahya Avatar answered Nov 04 '22 02:11

yahya