0

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?

4
  • 1
    Firstly, I would start using generics: List<double[]> alParent = new ArrayList<>();. Next, have a look at the ArrayList documentation - see which of those methods sounds like it will get a specific element... Commented Sep 15, 2016 at 5:42
  • List.get(index) returns item at a specific index and then select desired double value from array. for example alParent.get(0)[0] will return fist arrays 0th element i.e. 15.22 Commented Sep 15, 2016 at 5:45
  • @JonSkeet I am not looking for direct method. Is there any other way i can do this? Commented Sep 15, 2016 at 5:47
  • 1
    What do you mean by "I am not looking for direct method"? It's not clear what you're expecting here, but alParent.get(row)[column] is about as simple as it gets... if you want all the elements in a given column, just iterate over the rows... Commented Sep 15, 2016 at 5:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Suppose we have an array, arr = [2, 4, 5, 6], and we want to store all elements into an empty ArrayList.

int arr[] = {2,4,5,6}; 
ArrayList<Integer> al =  new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++){
    int num = arr[i];
    al.add(i, num);
}

// print the value of ArrayList 
for (int i = 0; i < arr.length; i++){
    System.out.println(al.get(i));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.