Raj Tandel Raj Tandel - 2 months ago 9
Java Question

How to access specific element of array stored in arraylist?

I have following code

List<double[]> alParent = new ArrayList<>();
alParent.add(new double[]{15.22,25.22});
alParent.add(new double[]{1.1,2.3});
double tempone[] = {1,2};
double temptwo[] = {4,5};
alParent.add(tempone);
alParent.add(temptwo);


I have stored array of type
double[]
inside arraylist.
I want to access elements from arraylist which are on first position and second position
double[]
array. For my example I want to access
15.22,1.1,1,4
and
25.22,2.3,2,5
separately. Is there any way i can get specific elements stored inside arraylist?
Also, if i have more elements in arraylist how do i get my desire result?

Answer

use blow code:

double temp1[] = new double[alParent.size()];    
double temp2[] = new double[alParent.size()];    
for (int i = 0; i < alParent.size(); i++) {    
    temp1[i] = alParent.get(i)[0];    
    temp2[i] = alParent.get(i)[1];    
}

temp1 and temp2 are arrays you want.